Convert a Float NumPy Array to Integer Array in NumPy

In this tutorial, we will introduce how to convert a float numpy array to integer array.

Convert a Float NumPy Array to Integer Array in NumPy

1. The most simplest way is to use dtype.

import numpy as np
cocyer_float_list = [45.45,84.75,69.12]
array_int = np.array(cocyer_float_list , dtype='int')
print(array_int)

We will convert float array into integer. array_int will be;

[45 84 69]

2. Use numpy ndarray.astype() function

We also can use ndarray.astype() function to convert a float array to a new integer array.

import numpy as np
cocyer_float_list = [45.45,84.75,69.12]
cocyer_array = np.array(cocyer_float_list )
print(cocyer_array.astype('int'))

The output will be:

[45 84 69]