In this article, we will see how to convert the NumPy Array of Floats into Integers. We are given a NumPy array of float-type values. Our task is to convert all the float-type values of the Numpy array to their nearest array of integer values.
Input1: [1.2 4.5 9.1 6.5 8.9 2.3 1.2]
Output1: [1 4 9 6 8 2 1]
Input2: [ 1.2 3.4 5.6 7.8 9.1 11.2 14.5 16.7]
Output2: [ 1 3 5 7 9 11 14 16]
Convert NumPy Array of Floats into Integers in 1-D Array
There are some methods to convert the float values in our NumPy array to the array of nearest integer values in Python. Here are some of the methods mentioned below :
- Naive Approach
- Using numpy.astype()
- Using numpy.asarray()
- Using np.int_()
Naive Approach
By explicitly type casting we can convert each element of the original Numpy array to its closest integer value. This technique converts a float value to an integer using the int() or round() methods. Use int() if you have to obtain floor values; otherwise, use round() to remove the decimal point and round to the nearest integer.
Python3
import numpy as np org = np.array([ 1.2 , 3.4 , 5.6 , 7.8 , 9.1 , 11.2 , 14.5 , 16.7 ]) #Displaying the original array print ( "Original Array : " ) print (org, "\n" ) #coverting array's float value to its integer value using int() integer_array = np.array([ int (i) for i in org]) #Displaying the result print ( "Integer Array : " ) print (integer_array, "\n" ) #coverting array's float value to its integer value using round() round_array = np.array([ round (i) for i in org]) #Displaying the result print ( "Round Array : " ) print (round_array) |
Output
Original Array :
[ 1.2 3.4 5.6 7.8 9.1 11.2 14.5 16.7]
Integer Array :
[ 1 3 5 7 9 11 14 16]
Round Array :
[ 1 3 6 8 9 11 14 17]
Time complexity: O(N)
Auxiliary Space: O(N)
Using numpy.astype()
In this example, we are using numpy .astype(). By passing the desired data type in the method, we can convert it to any data type within the Numpy array. Along with numpy.astype(), round() must be used to obtain precise values. Let’s see the code now. We had added both the methods with and without round(). Now its solely up to you which you choose depending on your need.
Python3
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" ) #Just eliminating the integer part integer_array = org.astype( int ) #Displaying the result print ( "Integer Array : " ) print (integer_array, "\n" ) #converting for closest integer value round_array = np. round (org).astype( int ) #Displaying the result print ( "Round Array : " ) print (round_array) |
Output
Original Array :
[ 1.2 3.4 5.6 7.8 9.1 11.2 14.5 16.7]
Integer Array :
[ 1 3 5 7 9 11 14 16]
Round Array :
[ 1 3 6 8 9 11 14 17]
Time complexity: O(N)
Auxiliary Space: O(N)
Using numpy.asarray()
In this example, we are using numpy.asarray() to convert NumPy Array of floats into integers.
Python3
import numpy as np #creating the original array org = [ 1.2 , 4.5 , 9.1 , 6.5 , 8.9 , 2.3 , 1.2 ] org = np.array(org) #Displaying the original array print ( "Original Array : " ) print (org, "\n" ) #converting the array to its integer form new = np.asarray(org,dtype = "int" ) #Displaying the result print ( "New Array : " ) print (new) |
Output
Original Array :
[1.2 4.5 9.1 6.5 8.9 2.3 1.2]
New Array :
[1 4 9 6 8 2 1]
Time complexity: O(N)
Auxiliary Space: O(N)
Using np.int_()
In this example, we are using np.int_() to convert NumPy Array of floats into integers.
Python3
import numpy as np #creating the original array org = [ 1.2 , 4.5 , 9.1 , 6.7 , 8.9 , 2.8 , 1.7 ] org = np.array(org) #Displaying the original array print ( "Original Array : " ) print (org, "\n" ) #Just eliminating the integer part integer_array = np.int_(org) #Displaying the result print ( "Integer Array : " ) print (integer_array, "\n" ) #converting for closest integer value round_array = np.round_(org) round_array = np.int_(round_array) #Displaying the result print ( "Round Array : " ) print (round_array) |
Output
Original Array :
[1.2 4.5 9.1 6.7 8.9 2.8 1.7]
Integer Array :
[1 4 9 6 8 2 1]
Round Array :
[1 4 9 7 9 3 2]
Time complexity: O(N)
Auxiliary Space: O(N)
Python NumPy Array of Floats into Integers in 2-D Array
There are some methods to convert the float values in our numpy array to the array of nearest integer values. Here are some of the methods mentioned below :
- Naive Approach
- Using numpy.astype()
- Using numpy.asarray()
- Using np.int_()
Naive Approach.
This implementation is same as we saw in 1-D array. We are here using explicit type casting too.
Python3
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 ]]) #displaying the original array print ( "Original Array : " ) print (org, "\n" ) # Create an empty list 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) #displaying the integer array print ( "Integer Array : " ) print (integer) |
Output
Original Array :
[[1.2 2.3 3.4]
[0.1 1.3 2.6]
[1.5 4.5 9.2]]
Integer Array :
[[1 2 3]
[0 1 2]
[1 4 9]]
Time complexity: O(N*M) , where N is no. of rows and M is no. of columns
Auxiliary Space: O(N*M) , where N is no. of rows and M is no. of columns
Using numpy.astype()
In this example, we are using numpy.astype(), similar to that of the 1-D array. Just call the method to the whole 2-D array at once. Now lets move to the code implementation.
Python3
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 ]]) #displaying the original array print ( "Original Array : " ) print (org, "\n" ) #applying .astype() integer = org.astype( int ) #displaying the integer array print ( "Integer Array : " ) print (integer) |
Output
Original Array :
[[1.2 2.3 3.4]
[0.1 1.3 2.6]
[1.5 4.5 9.2]]
Integer Array :
[[1 2 3]
[0 1 2]
[1 4 9]]
Time complexity: O(N*M) , where N is no. of rows and M is no. of columns
Auxiliary Space: O(N*M) , where N is no. of rows and M is no. of columns
Using numpy.asarray()
Its too has the same use case as the previous one in 1-D array. It takes whole 2-D array at once and then convert it into the desired data type which is passed through the function. Now lets move to code implementation.
Python3
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 ]]) #displaying the original array print ( "Original Array : " ) print (org, "\n" ) #applying .asarray() with dtype (data type) as integer integer = np.asarray(org, dtype = int ) #displaying the integer array print ( "Integer Array : " ) print (integer) |
Output
Original Array :
[[1.2 2.3 3.4]
[0.1 1.3 2.6]
[1.5 4.5 9.2]]
Integer Array :
[[1 2 3]
[0 1 2]
[1 4 9]]
Time complexity: O(N*M) , where N is no. of rows and M is no. of columns
Auxiliary Space: O(N*M) , where N is no. of rows and M is no. of columns
Using np.int_()
We had seen np.int_() implementation in 1-D array part. Its same for the 2-D array too. Now lets see the code implementation.
Python3
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 ]]) #displaying the original array print ( "Original Array : " ) print (org, "\n" ) #applying int_() to org array integer = np.int_(org) #displaying the integer array print ( "Integer Array : " ) print (integer) |
Output
Original Array :
[[1.2 2.3 3.4]
[0.1 1.3 2.6]
[1.5 4.5 9.2]]
Integer Array :
[[1 2 3]
[0 1 2]
[1 4 9]]
Time complexity: O(N*M) , where N is no. of rows and M is no. of columns
Auxiliary Space: O(N*M) , where N is no. of rows and M is no. of columns