Convert 2D float array to 2D int array in NumPy
Last Updated :
02 Oct, 2023
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
# creating 2D float array.
float_array = np.array([[1., 2., 3.], [4., 5., 6.]])
print('Before converting: ', float_array, '\n Data type of array: ', float_array.dtype)
# using astype() method to convert the data type of float_array from float to int
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
# creating 2D float array.
float_array = np.array([[1., 2., 3.], [4., 5., 6.]])
print('Before converting: ', float_array, '\n Data type of array: ', float_array.dtype)
# setting dtype to np.int32 to convert the data type of float_array from float to int
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
# importing numpy library
import numpy as np
# creating a 2D float array
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()
# Rounding the array
arr_2dim = arr_2dim.round()
print('After rounding:\n', arr_2dim, '\nData-type of the array: ', arr_2dim.dtype)
print()
# After rounding to the nearest integer, the data-type of the array is still float.
# Therefore, to convert it, we can type-cast.
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
# importing numpy library
import numpy as np
# creating a 2D float array
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()
# using the floor function on the array
arr_2dim = np.floor(arr_2dim)
print('After using the floor function:\n', arr_2dim, '\nData-type of the array: ', arr_2dim.dtype)
print()
# After flooring to the nearest, smaller integer, the data-type of the array is still float.
# Therefore, to convert it, we can type-cast.
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
# importing numpy library
import numpy as np
# creating a 2D float array
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()
# using the ceil function on the array
arr_2dim = np.ceil(arr_2dim)
print('After using the ceil function:\n', arr_2dim, '\nData-type of the array: ', arr_2dim.dtype)
print()
# After ceiling to the nearest, greater integer, the data-type of the array is still float.
# Therefore, to convert it, we can type-cast.
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
# importing numpy library
import numpy as np
# creating a 2D float array
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()
# using the truncate method on the array
arr_2dim = np.trunc(arr_2dim)
print('After using the trunc function:\n', arr_2dim, '\nData-type of the array: ', arr_2dim.dtype)
print()
# After truncating to the nearest integer to the zero than the element is, the data-type of the array is still float.
# Therefore, to convert it, we can type-cast.
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
# importing numpy library
import numpy as np
# creating a 2D float array
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()
# After type-casting.
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.
Similar Reads
Convert a 1D array to a 2D Numpy array
Here we will learn how to convert 1D NumPy to 2D NumPy Using two methods. Numpy is a Python package that consists of multidimensional array objects and a collection of operations or routines to perform various operations on the array and processing of the array. Convert a 1D array to a 2D Numpy arra
3 min read
Python | Flatten a 2d numpy array into 1d array
Given a 2d numpy array, the task is to flatten a 2d numpy array into a 1d array. Below are a few methods to solve the task. Method #1 : Using np.flatten() C/C++ Code # Python code to demonstrate # flattening a 2d numpy array # into 1d array import numpy as np ini_array1 = np.array([[1, 2, 3], [2, 4,
2 min read
How to Convert NumPy Array of Floats into Integers
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
4 min read
NumPy | Multiply 2D Array to 1D Array
Given two NumPy arrays, the task is to multiply a 2D array with a 1D array, each row corresponding to one element in NumPy. You can follow these methods to multiply a 1D array into a 2D array in NumPy: Using np.newaxis()Using axis as noneUsing transpose()Let's understand them better with Python prog
2 min read
How to convert 1D array of tuples to 2D Numpy array?
In this article, we will discuss how to convert a 1D array of tuples into a numpy array. Example: Input: [(1,2,3),('Hi','Hello','Hey')] Output: [['1' '2' '3'] ['Hi' 'Hello' 'Hey']] #NDArray Method 1: Using Map The map is a function used to execute a function for each item in an Iterable i.e array. E
2 min read
How to convert a dictionary into a NumPy array?
It's sometimes required to convert a dictionary in Python into a NumPy array and Python provides an efficient method to perform this operation. Converting a dictionary to NumPy array results in an array holding the key-value pairs in the dictionary. Python provides numpy.array() method to convert a
3 min read
Convert a NumPy array to an image
Converting a NumPy array to an image is a simple way to turn numbers into pictures. A NumPy array holds pixel values, which are just numbers that represent colors. Images, like PNG or JPEG, store these pixel values in a format we can see. In this process, the NumPy array turns into an image, with ea
3 min read
How to convert 1-D arrays as columns into a 2-D array in Python?
Let's see a program to convert 1-D arrays as columns into a 2-D array using NumPy library in Python. So, for solving this we are using numpy.column_stack() function of NumPy. This function takes a sequence of 1-D arrays and stack them as columns to make a single 2-D array. Syntax : numpy.column_stac
1 min read
How to Copy NumPy array into another array?
Many times there is a need to copy one array to another. Numpy provides the facility to copy array using different methods. In this Copy NumPy Array into Another ArrayThere are various ways to copies created in NumPy arrays in Python, here we are discussing some generally used methods for copies cre
3 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