Without further ado, let’s go exploring.
Function Syntax
Like most NumPy functions, the floor function has a simple syntax with lots of parameters, as shown below:
Parameters
Despite the many parameters, you will often find yourself using three parameters at a time.
Let us discuss some standard parameters in the function.
- x – refers to the input array.
- out – specifies an alternative array to store the output result.
- dtype – specifies the target output data type.
- where – the condition that is broadcasted over the input array.
- **kwargs – keyword-only arguments. Check the docs here.
Return Value
The function will return an array holding the floor values of each element in the array: Yeap, it’s that simple.
Example 1
Let us show how the function works with some basic examples:
import numpy as np
arr = np.array([-1.4, 1.2, -0.91, 34.2])
print(np.floor(arr))
This should return an array of absolute values of each element in the collection.
The resulting output array is as shown:
Example 2
The example below shows how to use the floor function with a 2D array.
print(np.floor(arr_2d))
This should return:
[-4. 0.]]
Conclusion
This was a short tutorial illustrating how to use the NumPy function to get the floor values of each element in an array.
Happy debugging 😊