NumPy floor() function
In this tutorial, we will cover the numpy.floor()
function which is a mathematical function in the Numpy library.
The numpy.floor()
is used to return the floor value of the elements of an array. The floor value of any given scalar value x is the largest integer value, such that i <= x
. For example, the floor value for 2.3 will be 2.
Syntax of numpy.floor()
:
Below we have a basic syntax to use this function and it is as follows:
numpy.floor(array)
The parameter named array is used to denote the array whose floor value needs to be calculated.
Returned values:
This method will return an array that contains floor values only.
Note: In some spreadsheet programs value will be calculated through "floor-towards-zero" way that means floor(-2.8) == -2. But in case of Numpy, the definition of floor is the opposite, floor(-2.8) == -3, so we always pick the closest lower integer value to ind the floor.
Now its time to cover a few examples in order to understand this example.
Example 1:
Below we have a code snippet for the example. Let us take a look at it:
import numpy as np
a = [0.23,-1.7,1.34,-2.334]
print("The array is :",a)
y = np.floor(a)
print("After applying the floor() method:",y)
The array is : [0.23, -1.7, 1.34, -2.334]
After applying the floor() method: [ 0. -2. 1. -3.]
Example 2:
Now here is another example for the floor()
function:
import numpy as np
input_arr = [1.23,0.22,-0.111,-2.555,-3.86,5.0,6.9]
print("The Input array is:")
print(input_arr)
z = np.floor(input_arr)
print("The output array is:")
print(z)
The Input array is:
[1.23, 0.22, -0.111, -2.555, -3.86, 5.0, 6.9]
The output array is:
[ 1. 0. -1. -3. -4. 5. 6.]
Summary:
This tutorial was all about numpy.floor()
mathematical function in the Numpy library which is used to calculate the floor value for all the array elements.