Do you want to convert the value of numpy float to int? If yes then this tutorial on “how to” is for you. In this entire post, you will learn various methods for converting numpy value from float to int. Let’s 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 the numpy module. So let’s import them using the import statement.

`import numpy as np`

### Step 2: Create a Numpy array.

Before converting numpy values from float to int. Let’s create both Single and Two-Dimensional Arrays.

You can create an 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])`

**Output**

`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]])`

**Output**

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

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.

Please note that in all the methods I am using the created numpy arrays in 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 a second method for converting numpy elements to int. And it is the * 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 to pass your float array with the

*as an argument inside the function.*

**dtype=”int”**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 what 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 another query then you can contact us.

Source:

#### Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.