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

Convert Numpy Float to Int

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

Numpy float to int for 1D Array
1D Array Conversion

For 2D Array

array_2d.astype(int)

Output

2D Array Conversion
2D Array Conversion

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.

numpy int
numpy int

Execute the following lines of code to convert.

For 1D Array

np.int_(array_1d)

Output

Using the numpy.int_() method for 1D Array
Using the numpy.int_() method for 1D Array

For 2D Array

np.int_(array_2d)

Output

Using the numpy.int_() method for 2D Array
Using the numpy.int_() method for 2D Array

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 dtype=”int” as an argument 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

Use of numpy.asarray() with the dtype for 1D Array
Use of numpy.asarray() with the dtype for 1D Array

For 2D Array

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

Output

Use of numpy.asarray() with the dtype for 2D Array
Use of numpy.asarray() with the dtype for 2D Array

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:

numpy.ndarray.astype

numpy.asarray

Join our list

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

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner