How to convert array from float to int in Numpy?

Learn together how to convert array from float to int in Numpy Python library.
numpy convert array from float to int

How to convert numpy array to int

To convert data type from float to int you need to use astype Numpy function and set ‘int’ as a function parameter.

import numpy as np

float_array = np.array([1.23, 12.5, 140.55, 3.14, 5.15])
print(f"My float array is: \n{float_array}")

int_array = float_array.astype('int')
print(f"My int array: \n {int_array}")

Alternatively you could round your values instead of converting data type from float to integer.

See also  How to use Numpy genfromtxt function?