Table of Contents
A NumPy array of floats can be converted into integers through the use of the “astype” method. This method allows for the array to be converted to a specified data type, in this case, integers. By specifying the desired data type as “int”, the array will be converted from floats to integers, rounding down any decimal values. This conversion can be useful in situations where integer values are needed for calculations or for storage purposes.
Convert NumPy Array of Floats into Integers
You can use the following methods to convert a NumPy array of floats to an array of integers:
Method 1: Convert Floats to Integers (Rounded Down)
rounded_down_integer_array = float_array.astype(int)
Method 2: Convert Floats to Integers (Rounded to Nearest Integer)
rounded_integer_array = (np.rint(some_floats)).astype(int)
Method 3: Convert Floats to Integers (Rounded Up)
rounded_up_integer_array = (np.ceil(float_array)).astype(int)
The following examples show how to use each method in practice with the following NumPy array of floats:
import numpy as np #create NumPy array of floats float_array = np.array([2.33, 4.7, 5.1, 6.2356, 7.88, 8.5]) #view array print(float_array) [2.33 4.7 5.1 6.2356 7.88 8.5 ] #view dtype of array print(float_array.dtype) float64
Example 1: Convert Floats to Integers (Rounded Down)
The following code shows how to convert a NumPy array of floats to an array of integers in which each float is rounded down to the nearest integer:
#convert NumPy array of floats to array of integers (rounded down)
rounded_down_integer_array = float_array.astype(int)
#view array
print(rounded_down_integer_array)
[2 4 5 6 7 8]
#view dtype of array
print(rounded_down_integer_array.dtype)
int32Notice that each float has been rounded down to the nearest integer and the new array has a dtype of int32.
Example 2: Convert Floats to Integers (Rounded to Nearest Integer)
The following code shows how to convert a NumPy array of floats to an array of integers in which each float is rounded to the nearest integer:
#convert NumPy array of floats to array of integers (rounded to nearest)
rounded_integer_array = (np.rint(float_array)).astype(int)
#view array
print(rounded_integer_array)
[2 5 5 6 8 8]
#view dtype of array
print(rounded_integer_array.dtype)
int32Notice that each float has been rounded to the nearest integer and the new array has a dtype of int32.
Example 3: Convert Floats to Integers (Rounded Up)
The following code shows how to convert a NumPy array of floats to an array of integers in which each float is rounded up to the nearest integer:
#convert NumPy array of floats to array of integers (rounded up)
rounded_up_integer_array = (np.ceil(float_array)).astype(int)
#view array
print(rounded_up_integer_array)
[3 5 6 7 8 9]
#view dtype of array
print(rounded_up_integer_array.dtype)
int32Notice that each float has been rounded up to the nearest integer and the new array has a dtype of int32.
The following tutorials explain how to perform other common tasks in NumPy:
Cite this article
stats writer (2024). How can a NumPy array of floats be converted into integers?. PSYCHOLOGICAL SCALES. Retrieved from https://scales.arabpsychology.com/stats/how-can-a-numpy-array-of-floats-be-converted-into-integers/
stats writer. "How can a NumPy array of floats be converted into integers?." PSYCHOLOGICAL SCALES, 26 Jun. 2024, https://scales.arabpsychology.com/stats/how-can-a-numpy-array-of-floats-be-converted-into-integers/.
stats writer. "How can a NumPy array of floats be converted into integers?." PSYCHOLOGICAL SCALES, 2024. https://scales.arabpsychology.com/stats/how-can-a-numpy-array-of-floats-be-converted-into-integers/.
stats writer (2024) 'How can a NumPy array of floats be converted into integers?', PSYCHOLOGICAL SCALES. Available at: https://scales.arabpsychology.com/stats/how-can-a-numpy-array-of-floats-be-converted-into-integers/.
[1] stats writer, "How can a NumPy array of floats be converted into integers?," PSYCHOLOGICAL SCALES, vol. X, no. Y, ص Z-Z, June, 2024.
stats writer. How can a NumPy array of floats be converted into integers?. PSYCHOLOGICAL SCALES. 2024;vol(issue):pages.
