Here are three ways to convert NumPy Array of Floats into Integers:
- Using numpy.astype()
- Using numpy.int_()
- Using numpy.asarray() with a dtype
Method 1: Use numpy.astype()
Here are three different techniques using the np.astype() method.
Technique 1: Converting float to integer (Rounded Down)
The astype(int) method truncates any decimal part and return an array of integers.
Visual Representation
Example
import numpy as np
float_array = np.array([3.14, 2.71, 1.62])
rounded_down_integer_array = float_array.astype(int)
print(rounded_down_integer_array)
print(rounded_down_integer_array.dtype)
Output
[3 2 1]
int64
Technique 2: Converting float to integers (Rounded to Nearest Integer)
The np.rint() method rounds each element to its nearest integer and return a float array.
Visual Representation
Example
import numpy as np
float_array = np.array([3.14, 2.71, 1.62])
rounded_nearest_integer_array = np.rint(float_array).astype(int)
print(rounded_nearest_integer_array)
print(rounded_down_integer_array.dtype)
Output
[3 3 2]
int64
Technique 3: Converting float to integer (Rounded Up)
The np.ceil() method round up, each element to its nearest integer and return a float array.
Visual Representation
Example
import numpy as np
float_array = np.array([3.14, 2.71, 1.62])
rounded_up_integer_array = np.ceil(float_array).astype(int)
print(rounded_up_integer_array)
print(rounded_up_integer_array.dtype)
Output
[4 3 2]
int64
Converting a 2D numpy array of float to int
Example
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
Method 2: Using np.int_()
The np.int_ type truncates the decimal parts of floats, similar to using the astype(int) method.
Visual Representation
Example
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
Method 3: Use of 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.
Visual Representation
Example
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]
Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.