How to convert NumPy array from float to int

You can convert the NumPy array from float to int with the following code. If you want to learn Python, I highly recommend reading This Book.

How to convert NumPy array from float to int
How to convert NumPy array from float to int
# Import the NumPy library
import numpy as np

# Initialize the array
a = np.array([5.2, 3.1, 12.7, 20.9])

# Convert the float to integer
b = a.astype(int)

# Display the conversion
print(b)

Output:

[ 5  3 12 20]

Free Learning Resources

Leave a Comment

Your email address will not be published.