Manipulating Data Types in NumPy

Learn via video courses
Topics Covered

Overview

A major part of the journey of mastering programming is to master its basics. Be it any programming language, unless you master the basics, you won't be able to establish command over it. One of the most fundamental and crucial tools in any programming language is datatype manipulation, which enables us to change any type of data into the type that we choose.

Introduction

As discussed earlier, manipulating datatypes in NumPy happens to be one of the most fundamental blocks of mastering Python.

Variables can store data in a lot of different types, and these types can do a lot of different things in Python. Sometimes, changing the datatype of a variable might help us with a problem. Suppose you are looking to reverse an array. We can easily do that by converting the array to a string and reversing it.

Changing the Data Types of a NumPy Array

In Python, we can easily change the data type of an array using the astype() function. The dtype is an argument that the function accepts. All built-in and generic data types are supported by the function.

To demonstrate the use of astype(), we will be looking into some examples:

  • Changing data type from int32 to float64

Output

  • Changing data type from int32 to complex128

Output

Different Operations on Data Types in NumPy

In this section, we will cover the various operations that can be performed on DataTypes in NumPy:

Changing the Shapes and Dimensions

The ability of NumPy arrays to change their shapes and dimensions is paramount, as according to our needs, we can manipulate them anytime. When it comes to Deep Learning, certain models take in a specified shape of arrays, and in this case, NumPy helps.

There are a lot of methods to change the shape/dimensions of NumPy arrays. In this section, we will cover two of them:

  • Using shape() method

Output

  • Using reshape() function

The reshape() function is quite similar to shape(). The only difference is that in reshape(), the output is dependent on a variable. The variable can be anything between C, F, and A.

C and F: The index is chosen according to the available size in a memory block. A: The index is chosen by NumPy based on the best possible order.

Output

Element-wise Operations in NumPy

There are a lot of operations in NumPy that can be performed for elements in order. Some of these functions are add(), divide(), multiply(), and power().

  • add() function

This function will add all the elements of a NumPy array element-wise.

Output

  • divide() function

This function will divide all the elements of a NumPy array element-wise.

Output

  • multiply() function

This function will multiply all the elements of a NumPy array element-wise.

Output

  • power() function

    The first array elements are raised to powers from the second array, element-wise.

Output

Matrix Multiplication

Utilizing Numpy for multiplication is also referred to as vectorization, whose major goal is to minimize or eliminate the explicit use of for loops in the program to speed up calculation.

Output

Transpose of a Matrix

By flipping a matrix's rows and columns, one can determine the matrix's transpose. NumPy, being the efficient library that it is, has a built-in function that allows users to find the transpose with the help of the transpose() function.

Output

What is np.astype() Function in NumPy?

Introduction

A NumPy array in Python has a data type called dtype, which can be specified when creating an array using the np.array() function. To change the data type of an array, we use the astype() function that is provided by NumPy in Python.

Syntax

Parameters

dtype: The type code (or data type) to which the array should be cast. This is a mandatory parameter.

order: Regulates the result array's memory layout order. This is an optional parameter.

Changing the Datatype using np.astype()

To demonstrate the working of astype(), we will take a NumPy array of float values, and we'll convert it using astype():

As we can see, we have successfully converted a float NumPy array to an integer NumPy array using the astype() function.

Code Examples

To further cement the working of the astype() function, we will dive into some examples:

  • Float to Integer

Output

  • Float to String

Output

Conclusion

  • In this article, we learned about the astype() function, a function that helps us to change the datatype of any variable.
  • To demonstrate its use, we converted the datatype of a NumPy array using the astype() function.
  • We also understood the different operations that can be performed on NumPy arrays in Python, like transposing and computing the product of two matrices.
  • To conclude, we went through a deep dive into the astype() function, its parameters, and some examples.