Numpy astype() method is the most common and efficient way to convert a numpy array of floats to an array of integers in Python. It provides explicit control over the data type, and you can cast it to any compatible type.
import numpy as np float_array = np.array([3.14, 2.71, 1.62]) print(float_array.dtype) # Output: float64 integer_array = float_array.astype(int) print(integer_array) # Output: [3 2 1] print(integer_array.dtype) # Output: int64
As shown in the above output, the astype(int) method truncates any decimal part and returns an array of integers. Some algorithms, like indexing or classification, require large integers instead of floating-point.
Rounding to the nearest integer
If you want to avoid truncation, you can use the np.rint() method that rounds floats to the nearest integer before conversion.
Now, you may wonder why we need to use the ndarray.astype() function after the np.rint() method, as it seems we already have an integer.
Well, np.rint() method does not really return an integer, even though it looks like a whole number, it is still a floating value. After using ndarray.astype() method, we have an array of integers.
import numpy as np float_array = np.array([3.14, 2.71, 1.62]) print(float_array.dtype) # Output: float64 rounded_nearest_integer_array = np.rint(float_array).astype(int) print(rounded_nearest_integer_array) # Output: [3 3 2] print(rounded_nearest_integer_array.dtype) # Output: int64
You can see that we already rounded up to the nearest integer and then converted to type int64.
Rounding up
The np.ceil() method rounds up each element to its nearest integer and returns a float array. Then, we can convert that array of floating-point values to integer values using the .astype() method.
import numpy as np float_array = np.array([3.14, 2.71, 1.62]) print(float_array.dtype) # Output: float64 rounded_up_integer_array = np.ceil(float_array).astype(int) print(rounded_up_integer_array) # Output: [4 3 2] print(rounded_up_integer_array.dtype) # Output: int64
Since we are using the ceiling function, 3.14 becomes 4, 2.71 becomes 3, and 1.62 becomes 2. The final array is an integer array, and we verified its type using the .dtype attribute.
Converting a 2D numpy array of floats to int
Let’s define a 2D numpy array and convert it to a 2D integer array. You don’t need to do anything extra; just use the .astype(int) method on the input numpy array.
import numpy as np
float_arr = np.array([[11.21, 19.21],
[46.21, 18.21],
[29.21, 21.21]])
print(float_arr)
print('After converting numpy float array to int array')
int_arr = float_arr.astype(int)
print(int_arr)
print("The data type of int_array is: ")
print(int_arr.dtype)
# Output:
# [[11.21 19.21]
# [46.21 18.21]
# [29.21 21.21]]
# After converting numpy float array to int array
# [[11 19]
# [46 18]
# [29 21]]
# The data type of int_array is:
# int64
Alternate approaches
Approach 1: Using np.int_()
The np.int_ type truncates the decimal parts of floats, similar to using the .astype(int) method.
import numpy as np
float_array = np.array([[11.21, 19.21],
[46.21, 18.21],
[29.21, 21.21]])
print(float_array)
print('After converting numpy float array to int array')
int_array = np.int_(float_array)
print(int_array)
print("The data type of int_array is: ")
print(int_array.dtype)
# Output:
# [[11.21 19.21]
# [46.21 18.21]
# [29.21 21.21]]
# After converting numpy float array to int array
# [[11 19]
# [46 18]
# [29 21]]
# The data type of int_array is:
# int64
Approach 2: Using numpy.asarray() with the dtype
Pass your float array along with dtype=’int’ as arguments to the np.asarray() method.
This dtype parameter will create a NumPy array of the specified integer type.
import numpy as np # Create a numpy array of float numbers float_array = np.array([3.14, 2.71, 1.62]) int_array = np.asarray(float_array, dtype=np.int32) print(int_array) # Output: [3 2 1]
That’s all!






