AppDividend
Latest Code Tutorials

How To Convert Numpy float to int Array in Python

Numpy astype() is a typecasting function that can be cast to a specified type. For example, if you want to convert your Numpy float array to int, you can use the astype() function. To make one of these into an int, or one of the other types in numpy, use the numpy astype() method.

Convert Numpy float to int array

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

Let’s convert step by step.

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 float array to 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.

Convert Numpy float array to int using np.int_() function

Numpy int_() us a Python type that represents a scalar NumPy type. You use it like Pythons int.

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)

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 the different methods here.

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

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

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

See the following code.

# 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 need to be careful when converting all the array values to another data type value. Numpy nan and numpy inf are floating-point values and can’t be meaningfully converted to int.

Convert Numpy array to complex number

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

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

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)

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

Numpy astype() function is relatively simple in its use is bundled along with other such utilizable functions, and we can convert from one standard data type to another data type. We have seen numpy float to int and float to a complex array. That is it for today.

See also

Python isnan()

Python isinf()

Python isinstance()

Leave A Reply

Your email address will not be published.

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