Python

NumPy Astype

NumPy” is a powerful numerical computing library in Python that is capable of handling large, multidimensional arrays and matrices. The “numpy.astype()” function is an incredibly useful feature that lets you easily convert the data type of a NumPy array to another data type.

In this article, we will provide an in-depth guide on the “numpy.astype()” function using numerous examples. Here are the contents of this Python blog:

What is the “numpy.astype()” Function in Python?

The “numpy.astype()” function is utilized to change the datatype of a NumPy array and returns a new array with the particular data type keeping the original array unchanged.

Syntax

numpy.ndarray.astype(dtype, order='K', casting= 'unsafe', subok= True, copy= True)

 

In the above syntax:

  • The “dtype” parameter specifies the new data type of the array. It can be a string (e.g., int, float, str, etc) or a NumPy data type object (e.g., np.int32, np.float64, np.str).
  • The optional parameter “order” specifies the memory layout of the output array.
  • The optional parameter “casting” specifies the type of casting rule to use. The default is “unsafe”.
  • The optional partner “subok” indicates whether to retrieve a subclass of the given array if possible. When set to “True”, a subclass is returned if it is possible.
  • The optional parameter “copy” determines whether to generate a new copy of the original array. It is set by default to “True”, causing a new copy to be created.

Example 1: Applying the numpy.astype()Function to Convert an Array of Float Type to an Integer Type

This example converts the given array of float elements into integer type using the “numpy.astype()” function:

import numpy
arr = numpy.array([1.2, 2.4, 3.6, 4.8])
new_arr = arr.astype(int)
print("Original array:", arr)
print("New array:", new_arr)

 

In the above code:

  • The “array()” function is used to create an array of float-type elements.
  • After that, the “astype()” function takes the integer as an argument and converts the original array to an integer type, thereby transforming the “float” array values into “int”.

Output

In the above output, the original array of floating-point numbers has been converted into the integer type successfully.

Example 2: Applying the numpy.astype()Function to Convert an Array of Float Type to Boolean Type

This example converts the initialized array of “int” type into “boolean”. Here is an example code:

import numpy
arr = numpy.array([1, 2, 3, 4])
new_arr = arr.astype(bool)
print("Original array:", arr)
print("New array:", new_arr)

 

In the above code block:

  • Likewise, the “array()” function is used to create an array with integer numbers.
  • The “astype()” function is used to convert the “int” array into a “boolean” data type by taking the “bool” specifier as its argument.

Output

In the above output, the original array of integers has been converted into the boolean type accordingly.

Example 3: Applying the numpy.astype()Function to Convert an Array of Integer Type to Complex Type

This example transforms the array of “int” type into a complex type via the “numpy.astype()” function:

import numpy
arr = numpy.array([1, 2, 3, 4])
new_arr = arr.astype(complex)
print("Original array:", arr)
print("New array:", new_arr)

 

In the above code snippet:

  • Similarly, the “array()” function is used to create an array of integers type values.
  • The “astype()” function is used to convert the given array into a “complex” type by accepting the complex specifier as its argument.

Output

In the above outcome, it can be implied that the complex array has been created from the original array of integers.

Example 4: Applying the numpy.astype()Function to Convert an Array of Integer Type to String Type

The following example converts the array values comprising the “int” type into “string” using the “numpy.astype()” function:

import numpy
arr = numpy.array([1, 2, 3, 4])
new_arr = arr.astype(str)
print("Original array:", arr)
print("New array:", new_arr)

 

In this code:

  • Recall the discussed approach for creating array integers.
  • Now, apply the “astype()” function that takes the “str” specifier as its argument and converts the “int” type array into a string.

Output

As observed, a “string” array has been retrieved from the original array of integers appropriately.

Conclusion

The “astype()” function of the “numpy” library is used to change the data type of a NumPy array into other data types such as “str”, “int”, “complex”, etc. We can modify a NumPy array from a float data type to an int, object, or complex type. This blog discussed a detailed explanation and example of the “numpy.astype()” function.

About the author

Talha Saif Malik

Talha is a contributor at Linux Hint with a vision to bring value and do useful things for the world. He loves to read, write and speak about Linux, Data, Computers and Technology.