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? Images are an easier way to represent the working model. In Machine Learning, Python uses the image data in the format of Height, Width, Channel format. i.e. Images are converted into Numpy Array in Height, Width, Channel format. Â In this article we will see How to Convert images to NumPy array? Mod
6 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
How to convert a list and tuple into NumPy arrays? In this article, let's discuss how to convert a list and tuple into arrays using NumPy. NumPy provides various methods to do the same using Python. Example: Input: [3, 4, 5, 6]Output: [3 4 5 6]Explanation: Python list is converted into NumPy ArrayInput: ([8, 4, 6], [1, 2, 3])Output: [[8 4 6] [1 2 3]
2 min read
Convert Python List to numpy Arrays NumPy arrays are more efficient than Python lists, especially for numerical operations on large datasets. NumPy provides two methods for converting a list into an array using numpy.array() and numpy.asarray(). In this article, we'll explore these two methods with examples for converting a list into
4 min read
How to Fix: ânumpy.float64â object cannot be interpreted as an integer In this article, we are going to see how to fix: Â ânumpy.float64â object cannot be interpreted as an integer. When a function or operation is applied to an object of the wrong type, a type error is raised. The 'numpy.float64' object cannot be interpreted as an integer is one example of this type of
2 min read
Convert a NumPy array into a CSV file After completing your data science or data analysis project, you might want to save the data or share it with others. Exporting a NumPy array to a CSV file is the most common way of sharing data. CSV file format is the easiest and most useful format for storing data and is convenient to share with o
3 min read