Python

# NumPy np.floor()

The NumPy floor function allows you to get the floor values of each numerical element in an array. The floor value refers to the nearest integer less than or equal to the real number.

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:

numpy.floor(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True[, signature, extobj]) = <ufunc 'floor'>

## 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.

1. x – refers to the input array.
2. out – specifies an alternative array to store the output result.
3. dtype – specifies the target output data type.
4. where – the condition that is broadcasted over the input array.
5. **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
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:

[-2. 1. -1. 34.]

## Example 2

The example below shows how to use the floor function with a 2D array.

arr_2d = np.array([[-0.3141, 3.141], [-3.141, .3141]])
print(np.floor(arr_2d))

This should return:

[[-13.]
[-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 😊 