NumPy – Using ndarray.astype() method to change data type (5 examples)

Updated: February 26, 2024 By: Guest Contributor Post a comment

Introduction

NumPy, a cornerstone of Python’s scientific computing stack, offers the ndarray object for multidimensional arrays. One of its handy methods is astype(), which allows for data type conversions. This article delves into the ndarray.astype() method, showcasing its functionality through 5 progressively advanced examples.

What is astype() Used for?

The astype() method is used to cast a NumPy array from one data type to another. It’s particularly useful in data preprocessing, where ensuring data type consistency is crucial. astype() creates a new array (a copy), leaving the original array unchanged.

Basic Example: Converting Integer to Float

import numpy as np
arr = np.array([1, 2, 3, 4])
arr_float = arr.astype(np.float64)
print(arr_float)

Output:

[1.0, 2.0, 3.0, 4.0]

This example demonstrates the straightforward application of astype(). Starting from an integer array, we convert it to float64.

Example 2: Converting to Boolean

arr = np.array([1, 0, 1, 0])
arr_bool = arr.astype(bool)
print(arr_bool)

Output:

[ True, False, True, False]

Non-zero values are converted to True, while zeroes to False. This transformation is particularly useful for mask operations or filtering data.

Example 3: Changing Data Type and Shape

arr = np.arange(10)
arr_reshaped = arr.reshape((2, 5))
arr_float_reshaped = arr_reshaped.astype(np.float64)
print(arr_float_reshaped)

Output:

[[0., 1., 2., 3., 4., 5., 6., 7., 8., 9.]]

Here, we’re not only changing the data type but also the shape of the array, showcasing the flexibility of astype() in data preprocessing tasks.

Example 4: From Float to Complex

arr = np.random.rand(4)
arr_complex = arr.astype(np.complex128)
print(arr_complex)

Output:

[0.73195735+0.j, 0.25468168+0.j, 0.13382053+0.j, 0.46678655+0.j]

Switching to a complex data type can be essential when dealing with certain mathematical operations or scientific computations.

Example 5: Working with Strings

arr = np.array([1, 2, 3, -4])
arr_str = arr.astype(str)
print(arr_str)

Output:

['1' '2' '3' '-4']

This showcases how astype() can be used to convert numerical arrays into string arrays. It can be useful for generating labels or when preparing your data for certain visualizations.

Advanced Application: Conservation of Memory

One of the imperative uses of astype() is in the conservation of memory, particularly in the context of big data. Converting data from a 64-bit to a 32-bit type can halve the memory usage, enhancing performance.

arr_large = np.random.rand(1000000)
arr_small = arr_large.astype(np.float32)
print('Original size (bytes):', arr_large.nbytes)
print('Converted size (bytes):', arr_small.nbytes)

This example effectively demonstrates the importance of data type selection in optimizing resource utilization.

Conclusion

The astype() method is a powerful tool in NumPy for data type conversion, offering flexibility and efficiency in data manipulation. From basic conversions to advanced memory management, understanding how to leverage astype() will significantly enhance your data preprocessing skills.