 # How to Convert Numpy Float to Int : Use any of 3 Methods

Do you want to convert value of numpy float to int. If yes then this tutorial of “how to” is for you. In this entire post you will learn various method for converting numpy value from float to int. Lets get started.

## Step by step to convert Numpy Float to Int

### Step 1: Import all the required libraries.

In this entire coding tutorial I will use only numpy module. So lets import them using the import statment.

``import numpy as np``

### Step 2: Create Numpy array.

Before converting numpy values from float to int. Lets create both Single and Two-Dimensional Array.

You can create array using the np.array() method.

1D Numpy Array

``array_1d = np.array([1.1, 2.2, 3.3, 4.4, 5.5, 6.6])``

2D Numpy Array

``array_2d= np.array([[1.1,2.2,3.3],[4.4,5.5,6.6],[7.7,8.8,9.9]])``

The both arrays are of float type. In the next step I will show you the three methods to convert numpy float to int.

### Step 3: Use the below methods for numpy float to int conversion.

Plase note that in all the methods I am using the created numpy arrays in the step 2.

#### Method 1: Using astype(int) method.

You can convert numpy array elements to int using the astype() method. You have to just pass the entire array inside the function.

Run the below code.

For 1D Array

``array_1d.astype(int)``

Output

For 2D Array

``array_2d.astype(int)``

Output

#### Method 2:  Using the numpy.int_() method.

There is also second method for converting numpy elelments to int .And it is np.int_() method. Just pass your input array as an argument inside the method.

Execute the following lines of code to convert.

For 1D Array

``np.int_(array_1d)``

Output

For 2D Array

``np.int_(array_2d)``

Output

### Method 3:  Use of numpy.asarray() with the dtype.

The third method for converting elements from float to int is np.asarray(). Here you have pass your float array with the dtype=”int” as an arguments inside the function.

You will get the same output as the above methods. Just run the given lines of code.

For 1D Array

``np.asarray(array_1d,dtype="int")``

Output

For 2D Array

``np.asarray(array_2d,dtype="int")``

Output

## END NOTES:

These are the simple and basic methods for implementing the float to int conversion. The choice of selection is upon your convenience and likeness. If you ask which one to use then It depends. When you’re not sure whats your input array is of type, you can use asarray with dtype=int instead of astype.

If your input array already has the correct dtype, asarray avoids the array copy while astype does not.

Hope this entire tutorial has helped you in solving your question of how to convert numpy array from float to int. If you have other query then you can contact us.

Source:

numpy.ndarray.astype

numpy.asarray 