2 Ways to Convert Numpy float to int Array in Python

Converting a float value of an array to an int array is called Type conversion, an explicit method of converting an operand to a specific type.

Numpy float to int array

2 Easy ways to convert a float to an int array in Numpy.

  1. np.asarray(): It takes an array of float values and converts them into an integer array.
  2. np.int_(): It returns an array with elements of the default integer type.

Method 1: Use np.asarray()

To convert the numpy float to int array in Python, use the np.astype() function. The np.astype() is a numpy library function that takes an array of float values and converts it into an integer array.

Step 1: Create a numpy array

See the following code.

# app.py

import numpy as np

og_array = np.array([[11.21, 19.21], [46.21, 18.21], [29.21, 21.21]])
print(og_array)

Output

python3 app.py
[[11.21 19.21]
 [46.21 18.21]
 [29.21 10.21]]

Step 2: Convert Numpy float to int using numpy.astype()

Let’s convert a float array to an int array.

# app.py

import numpy as np

og_array = np.array([[11.21, 19.21], [46.21, 18.21], [29.21, 21.21]])
print(og_array)

print('After converting numpy float array to int array')
int_array = og_array.astype(int)
print(int_array)

Output

python3 app.py
[[11.21 19.21]
 [46.21 18.21]
 [29.21 21.21]]
After converting numpy float array to int array
[[11 19]
 [46 18]
 [29 21]]

From the output, you can see that all the values are converted to integer values.

Now, we will check the dtype of the given array object.

# app.py

import numpy as np

og_array = np.array([[11.21, 19.21], [46.21, 18.21], [29.21, 21.21]])
print(og_array)

print('After converting numpy float array to int array')
int_array = og_array.astype(int)
print(int_array)
print("The data type of int_array is: ")
print(int_array.dtype)

Output

python3 app.py
[[11.21 19.21]
 [46.21 18.21]
 [29.21 21.21]]
After converting numpy float array to int array
[[11 19]
 [46 18]
 [29 21]]
The data type of int_array is:
int64

We can see that the data type of the converted array is an integer.

Method 2: Use np.int_()

Numpy int_() is a Python type representing a scalar NumPy type.

# app.py

import numpy as np

og_array = np.array([[11.21, 19.21], [46.21, 18.21], [29.21, 21.21]])
print(og_array)

print('After converting numpy float array to int array')
int_array = np.int_(og_array)
print(int_array)
print("The data type of int_array is: ")
print(int_array.dtype)

Output

python3 app.py
[[11.21 19.21]
 [46.21 18.21]
 [29.21 21.21]]
After converting numpy float array to int array
[[11 19]
 [46 18]
 [29 21]]
The data type of int_array is:
int64

We will get the same output but different methods here.

But my suggestion is to use the numpy astype() function, which can convert standard data types.

Numpy astype() with np.inf() and np.nan()

In our original array, some values are not floating but np.inf or np.nan values.

# app.py

import numpy as np

og_array = np.array([[11.21, np.nan], [46.21, 18.21], [29.21, np.inf]])
print(og_array)

print('After converting numpy float array to int array')
int_array = og_array.astype(int)
print(int_array)

Output

python3 app.py
[[11.21   nan]
 [46.21 18.21]
 [29.21   inf]]
After converting numpy float array to int array
[[                  11 -9223372036854775808]
 [                  46                   18]
 [                  29 -9223372036854775808]]

From the output, you can see that the results are unexpected.

So, you must be careful when converting all the array values to another data type.

Numpy nan and numpy inf are floating-point values and can’t be meaningfully converted to int.

Converting Numpy array to complex number

The Numpy astype() function can convert any data type to another. However, it does not necessarily convert into particular data types.

Let’s convert the float data type to the ‘complex128′ type using numpy.astype().

# app.py

import numpy as np

og_array = np.array([[11.21, 19.21], [46.21, 18.21], [29.21, 21.21]])
print(og_array)

print('After converting numpy float array to complex array')
int_array = og_array.astype(complex)
print(int_array)
print("The data type of int_array is: ")
print(int_array.dtype)

Output

python3 app.py
[[11.21 19.21]
 [46.21 18.21]
 [29.21 21.21]]
After converting numpy float array to complex array
[[11.21+0.j 19.21+0.j]
 [46.21+0.j 18.21+0.j]
 [29.21+0.j 21.21+0.j]]
The data type of int_array is:
complex128

So, we have converted the numpy float array to a numpy complex array using the np astype() function.

Conclusion

The Numpy astype() function is relatively simple in its use and is bundled along with other such utilizable functions, and we can convert from one standard data type to another.

We saw numpy float to int and float to a complex array.

That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.