Numpy Floor in Python with Examples
Numpy Floor in Python with Examples

Numpy Floor in Python with Examples : What does it do ?

Numpy is a python module for doing array manipulation. In the module, there are many functions that allow you to do the mathematical calculation in a very fast way. Numpy floor is one of the functions of it. It allows you to convert the whole float NumPy array into an int. In this entire tutorial, you will know how to use it step by step.

Steps to Implement Numpy Floor

Step 1: Import Numpy library

You can import the NumPy library using the import statement.

import numpy as np

Step 2: Create a Sample Numpy array

In our tutorial, I am implementing a floor on both 1D and 2D arrays. So let’s create a sample of them.

1D Numpy array

array_1d = np.array([-2.3,1.4,-5.7,2.3,4.5])

Output

array([-2.3, 1.4, -5.7, 2.3, 4.5])

2D Numpy array

array_2d = np.array([[1.0,-2.3,4.5],[2.1,-6.3,-5.5],[4.5,-2.2,-5.5]])

Output

array([[ 1. , -2.3,  4.5],
       [ 2.1, -6.3, -5.5],
       [ 4.5, -2.2, -5.5]])

Step 3: Convert Numpy Float to int using floor

Now the last step is to convert all the float elements of the array to int using the floor() method.

Applying floor() on 1D NumPy array

Let’s apply the function to the one-dimensional NumPy array. Execute the below lines of code.

np.floor(array_1d)

Output

Applying floor() on 1D numpy array
Applying floor() on 1D array

You can see in the output all the floats element of the NumPy array has converted to int after applying floor on it.

Applying floor() on 2D NumPy array

Now let’s apply the same function on the 2D array.

np.floor(array_2d)

The output you will get is below.

Applying floor() on 2D array
Applying floor() on 2D array

That’s all! These are the examples to use the floor() method on the array. In the next section, you will know other examples to use this function.

Other Examples

NumPy floor of matrix

In this example, I will create an array and apply the floor() method to it. Execute all the below lines of code.

import numpy as np
matrix = np.matrix([[1.1,2.2,3.3],[-1.1,2.3,4.5]])
print(np.round(matrix))

Output

Applying floor() on Matrix
Applying floor() on Matrix

NumPy Floor vs Math Floor

Many data science learner readers have contacted me to explain the difference between the numpy floor and math floor. I want to tell them that the major difference between them is the output of the element. For example, If I will use math.floor(1.1) then I will get the output as 1 of type integer. But if I apply the np.floor(1.1) then I will get the output as 1.0. And it is of float type.

"Math

The second difference is that you cannot find the floor at once for the entire element in math.floor() whereas in the case of the np.floor() method you can do so.

Conclusion

These are the examples of using the floor() method on an array. Hope you have liked this article. If you have any queries then you can contact us. We are always ready to help you.

Source:

Floor Documentation

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