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
How to Convert NumPy Array of Floats into Integers
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]
Contact Us