How to Convert NumPy Array of Floats into Integers
Last Updated :
21 Apr, 2025
In this article, we will see how to convert NumPy Array of Floats into Integers. We are given a NumPy array of float-type values. Our task is to convert all float-type values of Numpy array to their nearest array of integer values.
Input: [1.2, 4.5, 9.1, 6.5, 8.9, 2.3, 1.2]
Output: [1, 4, 9, 6, 8, 2, 1]
Convert NumPy Array of Floats into Integers in 1-D Array
Various methods to convert float values in our NumPy array to array of nearest integer values in Python are mentioned below :
1. Naive Approach
A simple way to convert a float to an integer is by using the int() or round() function. Using int() will drop decimal part while using round() will round the float to nearest integer.
Python
import numpy as np
org = np.array([1.2, 3.4, 5.6, 7.8, 9.1, 11.2, 14.5, 16.7])
print("Original Array : ")
print(org, "\n")
integer_array = np.array([int(i) for i in org])
print("Integer Array : ")
print(integer_array, "\n")
round_array = np.array([round(i) for i in org])
print("Round Array : ")
print(round_array)
Output
Naive Approach2. Using numpy.astype()
We are using numpy.astype() which is one of the most efficient ways to convert a NumPy array from one data type to another. Along with numpy.astype(), round() must be used to obtain precise values. We had added both methods with and without round().
Python
import numpy as np
org = [1.2, 3.4, 5.6, 7.8, 9.1, 11.2, 14.5, 16.7]
org = np.array(org)
print("Original Array : ")
print(org, "\n")
integer_array = org.astype(int)
print("Integer Array : ")
print(integer_array, "\n")
round_array = np.round(org).astype(int)
print("Round Array : ")
print(round_array)
Output
numpy.astype3. Using numpy.asarray()
We are using numpy.asarray() to convert NumPy Array of floats into integers. It is similar to astype()
but it is slightly faster in some cases because it doesn’t need to create a copy of the array if the data type is already the same.
Python
import numpy as np
org = [1.2, 4.5, 9.1, 6.5, 8.9, 2.3, 1.2]
org = np.array(org)
print("Original Array : ")
print(org, "\n")
new = np.asarray(org, dtype="int")
print("New Array : ")
print(new)
Output
numpy.asarray()4. Using np.int_()
The np.int_()
method is another way to convert float arrays into integers. It works similarly to astype()
but it is a more specialized method for NumPy arrays.
Python
import numpy as np
org = [1.2, 4.5, 9.1, 6.7, 8.9, 2.8, 1.7]
org = np.array(org)
print("Original Array : ")
print(org, "\n")
integer_array = np.int_(org)
print("Integer Array : ")
print(integer_array, "\n")
round_array = np.round(org)
round_array = np.int_(round_array)
print("Round Array : ")
print(round_array)
Output
np.int_()Convert NumPy Array of Floats into Integers in 2-D Array
Now we will see how to convert numpy array into 2D integer array. We will use same methoda discussed above:
1. Naive Approach
This implementation is same as we saw in 1-D array. We are here using explicit type casting too here.
Python
import numpy as np
org = np.array([
[1.2, 2.3, 3.4],
[0.1, 1.3, 2.6],
[1.5, 4.5, 9.2]])
print("Original Array : ")
print(org, "\n")
new = []
for i in range(org.shape[0]):
helper = []
for j in range(org.shape[1]):
helper.append(int(org[i, j]))
new.append(helper)
integer = np.array(new)
print("Integer Array : ")
print(integer)
Output
Naive Approach In 2-D Array2. Using numpy.astype()
In this example, we are using numpy.astype() similar to 1-D array. Just call method to the whole 2-D array at once.
Python
import numpy as np
org = np.array([
[1.2, 2.3, 3.4],
[0.1, 1.3, 2.6],
[1.5, 4.5, 9.2]])
print("Original Array : ")
print(org, "\n")
integer = org.astype(int)
print("Integer Array : ")
print(integer)
Output
numpy.astype()3. Using numpy.asarray()
It takes whole 2-D array at once and then convert it into desired data type which is passed through the function.
Python
import numpy as np
org = np.array([
[1.2, 2.3, 3.4],
[0.1, 1.3, 2.6],
[1.5, 4.5, 9.2]])
print("Original Array : ")
print(org, "\n")
integer = np.asarray(org, dtype=int)
print("Integer Array : ")
print(integer)
Output
numpy.asarray()4. Using np.int_()
Python
import numpy as np
org = np.array([
[1.2, 2.3, 3.4],
[0.1, 1.3, 2.6],
[1.5, 4.5, 9.2]])
print("Original Array : ")
print(org, "\n")
integer = np.int_(org)
print("Integer Array : ")
print(integer)
Output
np.int_()Converting NumPy arrays of floats to integers can be done using various methods where each offers some advantages. However for simplicity and ease of use numpy.astype() becomes best choice for both 1-D and 2-D arrays.
Similar Reads
How to Convert images to NumPy array? Pictures on a computer are made of tiny dots called pixels. To work with them in Python, we convert them into numbers using a NumPy array is a table of numbers showing each pixelâs color. In this article, weâll learn how to do this using popular Python tools.Loading the images via Pillow LibraryLet
5 min read
How to convert NumPy array to list ? This article will guide you through the process of convert a NumPy array to a list in Python, employing various methods and providing detailed examples for better understanding. Convert NumPy Array to List There are various ways to convert NumPy Array to List here we are discussing some generally us
4 min read
How to convert Float to Int in Python? In Python, you can convert a float to an integer using type conversion. This process changes the data type of a value However, such conversions may be lossy, as the decimal part is often discarded.For example:Converting 2.0 (float) to 2 (int) is safe because here, no data is lost.But converting 3.4
5 min read
How to Convert NumPy Matrix to Array In NumPy, a matrix is essentially a two-dimensional NumPy array with a special subclass. In this article, we will see how we can convert NumPy Matrix to Array. Also, we will see different ways to convert NumPy Matrix to Array. Convert Python NumPy Matrix to an ArrayBelow are the ways by which we can
3 min read
Python | Filter out integers from float numpy array Given a numpy array, the task is to filter out integers from an array containing float and integers. Let's see few methods to solve a given task. Method #1 : Using astype(int)Â Python3 # Python code to demonstrate # filtering integers from numpy array # containing integers and float import numpy as
2 min read
Convert 2D float array to 2D int array in NumPy Converting a 2D float array to a 2D integer array in NumPy is a straightforward process using the astype() method. This conversion can be useful in various data analysis and scientific computing tasks where integer data types are required or where memory efficiency is essential. In this article, we
8 min read