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 are going to have a look at ‘How to convert a 2D float array to a 2D int array in NumPy?’
What are 2D arrays in Python?
2D arrays mean ‘2-Dimensional arrays’. These are the arrays that contain rows and columns, thus making them 2-dimensional.
Converting 2D float array to 2D int array in NumPy
Below are the topics that we will cover in this article:
- Using astype() method
- Setting the dtype of the array to np.int32
- Using ndarray.round() method.
- Using np.floor(ndarray) function.
- Using np.ceil(ndarray) function.
- Using np.trunc(ndarray) function.
- Type-casting the array directly to the np.int32 data-type.
Using astype() method
To convert 2D float array to int array, we can make use of the astype() method.
Syntax: integer_array = integer_array.astype(np.data_type)
Example: In the program, we have used astype() to convert the float array to int array. Initially, the data type of the array was ‘float’. In the astype() method, we gave the argument as np.int32 (integer data type) which changed the data type of the array.
Python3
import numpy as np
float_array = np.array([[ 1. , 2. , 3. ], [ 4. , 5. , 6. ]])
print ( 'Before converting: ' , float_array, '\n Data type of array: ' , float_array.dtype)
float_array = float_array.astype(np.int32)
print ( 'After Converting: ' , float_array, '\n Data type of array: ' , float_array.dtype)
|
Output
Before converting: [[1. 2. 3.]
[4. 5. 6.]]
Data type of array: float64
After Converting: [[1 2 3]
[4 5 6]]
Data type of array: int32
Setting the dtype of the array to np.int32
When we initialize an array with float elements, its dtype (data-type) is np.float64. To convert the data-type of the array to int, we can change the dtype to np.int32.
Python3
import numpy as np
float_array = np.array([[ 1. , 2. , 3. ], [ 4. , 5. , 6. ]])
print ( 'Before converting: ' , float_array, '\n Data type of array: ' , float_array.dtype)
float_array = np.array(float_array, dtype = np.int32)
print ( 'After Converting: ' , float_array, '\n Data type of array: ' , float_array.dtype)
|
Output:
Before converting: [[1. 2. 3.]
[4. 5. 6.]]
Data type of array: float64
After Converting: [[1 2 3]
[4 5 6]]
Data type of array: int32
In the above example, first, we created a 2D array with float elements. Then, we stored another array in the same variable (float_array) with the same elements by passing the float_array as argument in the np.array(). Finally, we changed the dtype of the array to np.int32. Thus, the 2D float array converted to 2D int array.
Using ndarray.round() method.
To convert the 2D float array to a 2D int array, we can first round the floating-point numbers to the nearest integer. This gives us a 2D float array as well but with rounded values. Then, we can finally convert it to the integer data-type.
Python3
import numpy as np
arr_2dim = np.array([ 1.35 , 2.75 , 3.50 , 4.0 , 5.9 , 6.85 ]).reshape( 2 , 3 )
print ( 'Before converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
print ()
arr_2dim = arr_2dim. round ()
print ( 'After rounding:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
print ()
arr_2dim = np.int32(arr_2dim)
print ( 'After converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
|
Output:
Before converting:
[[1.35 2.75 3.5 ]
[4. 5.9 6.85]]
Data-type of the array: float64
After rounding:
[[1. 3. 4.]
[4. 6. 7.]]
Data-type of the array: float64
After converting:
[[1 3 4]
[4 6 7]]
Data-type of the array: int32
In the above program, we first created a 2D float array. Then, we rounded it. Inside the round function, the default value of decimals to be rounded is ‘zero’. That is why, it rounded in the manner that there are no numbers but zero after the decimal point. But still, even after the rounding, the data-type of the array is float. Thus, we type-casted the array into integer data-type.
In the place of round function, we can also use ‘np.round_(ndarray)‘ function, which is nothing but the alias of the round function. It provides the exact same functionality as the round function.
Using np.floor(ndarray) function.
We can use np.floor(ndarray) to floor the elements in the array and then we can later type-cast it to the integer data-type.
np.floor(ndarray) : This function floors the elements in the array, i.e., converts the numbers to the nearest integer to them, but lesser than them.
Python
import numpy as np
arr_2dim = np.array([ 1.35 , 2.75 , 3.50 , 4.0 , 5.9 , 6.85 ]).reshape( 2 , 3 )
print ( 'Before converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
print ()
arr_2dim = np.floor(arr_2dim)
print ( 'After using the floor function:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
print ()
arr_2dim = np.int32(arr_2dim)
print ( 'After converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
|
Output:
Before converting:
[[1.35 2.75 3.5 ]
[4. 5.9 6.85]]
Data-type of the array: float64
After using the floor function:
[[1. 2. 3.]
[4. 5. 6.]]
Data-type of the array: float64
After converting:
[[1 2 3]
[4 5 6]]
Data-type of the array: int32
Using np.ceil(ndarray) function.
np.ceil(ndarray) : This function ceils the elements in the array, i.e., convert the numbers to the nearest integer to them, but greater than them.
We can, first, ceil the elements in the array and then later convert it to the integer data-type.
Python3
import numpy as np
arr_2dim = np.array([ 1.35 , 2.75 , 3.50 , 4.0 , 5.9 , 6.85 ]).reshape( 2 , 3 )
print ( 'Before converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
print ()
arr_2dim = np.ceil(arr_2dim)
print ( 'After using the ceil function:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
print ()
arr_2dim = np.int32(arr_2dim)
print ( 'After converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
|
Output:
Before converting:
[[1.35 2.75 3.5 ]
[4. 5.9 6.85]]
Data-type of the array: float64
After using the ceil function:
[[2. 3. 4.]
[4. 6. 7.]]
Data-type of the array: float64
After converting:
[[2 3 4]
[4 6 7]]
Data-type of the array: int32
Using the np.trunc(ndarray) function.
np.trunc(ndarray) : This function truncates the elements in the array, i.e., converts them to the integer that is closer to the zero than the element itself is. In other words, just the the numbers after the decimal point are removed.
Python3
import numpy as np
arr_2dim = np.array([ 1.35 , 2.75 , 3.50 , 4.0 , 5.9 , 6.85 ]).reshape( 2 , 3 )
print ( 'Before converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
print ()
arr_2dim = np.trunc(arr_2dim)
print ( 'After using the trunc function:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
print ()
arr_2dim = np.int32(arr_2dim)
print ( 'After converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
|
Output:
Before converting:
[[1.35 2.75 3.5 ]
[4. 5.9 6.85]]
Data-type of the array: float64
After using the trunc function:
[[1. 2. 3.]
[4. 5. 6.]]
Data-type of the array: float64
After converting:
[[1 2 3]
[4 5 6]]
Data-type of the array: int32
In the place of np.trunc() function, we can also use np.fix().
In all the above examples, we have used np.int32() to convert the data-type. We could also use astype() method for the same.
Type-casting the array directly to the np.int32 data-type.
We can also convert the elements of an array to the integer data-type by type-casting as given below:
ndarray = np.int32(ndarray)
Python3
import numpy as np
arr_2dim = np.array([ 1.35 , 2.75 , 3.50 , 4.0 , 5.9 , 6.85 ]).reshape( 2 , 3 )
print ( 'Before converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
print ()
arr_2dim = np.int32(arr_2dim)
print ( 'After converting:\n' , arr_2dim, '\nData-type of the array: ' , arr_2dim.dtype)
|
Output:
Before converting:
[[1.35 2.75 3.5 ]
[4. 5.9 6.85]]
Data-type of the array: float64
After converting:
[[1 2 3]
[4 5 6]]
Data-type of the array: int32
We can convert the 2D int array to 2D float array in the same manner.
Share your thoughts in the comments
Please Login to comment...