NumPy floor()
The numpy.floor()
function rounds each element of an array down to the nearest integer.
It returns the largest integer less than or equal to each element in the input.
Syntax
</>
Copy
numpy.floor(x, /, out=None, *, where=True, casting='same_kind', order='K', dtype=None, subok=True)
Parameters
Parameter | Type | Description |
---|---|---|
x | array_like | Input array whose elements will be floored. |
out | ndarray, None, or tuple of ndarray and None, optional | Optional output array where the result is stored. If None, a new array is created. |
where | array_like, optional | Boolean mask specifying which elements to apply the floor function to. |
casting | str, optional | Defines how casting should behave. |
order | str, optional | Memory layout order of the output array. |
dtype | data-type, optional | Defines the data type of the output array. |
subok | bool, optional | Determines if subclasses of ndarray are preserved in the output. |
Return Value
Returns an array where each element is rounded down to the nearest integer. If the input is a scalar, a scalar is returned.
Examples
1. Applying floor to a Single Value
Here, we compute the floor of a single floating-point value.
</>
Copy
import numpy as np
# Define a floating-point number
num = 3.7
# Apply the floor function
result = np.floor(num)
# Print the result
print("Floor of 3.7:", result)
Output:
Floor of 3.7: 3.0

2. Applying floor to an Array
We compute the floor values for multiple floating-point numbers provided in an array.
</>
Copy
import numpy as np
# Define an array of floating-point numbers
arr = np.array([1.2, 2.8, -3.5, 4.9, -1.1])
# Apply the floor function
floored_values = np.floor(arr)
# Print the results
print("Original array:", arr)
print("Floored values:", floored_values)
Output:
Original array: [ 1.2 2.8 -3.5 4.9 -1.1]
Floored values: [ 1. 2. -4. 4. -2.]

3. Using the out
Parameter
Using an output array to store results instead of creating a new array.
</>
Copy
import numpy as np
# Define an array of floating-point numbers
arr = np.array([5.7, 6.3, -2.9, -8.1])
# Create an output array with the same shape
output_array = np.empty_like(arr)
# Apply floor function and store the result in output_array
np.floor(arr, out=output_array)
# Print the results
print("Floored values stored in output array:", output_array)
Output:
Floored values stored in output array: [ 5. 6. -3. -9.]

4. Using the where
Parameter
Using a condition to apply floor only to selected elements.
</>
Copy
import numpy as np
# Define an array of floating-point numbers
arr = np.array([3.5, 7.8, -4.6, 2.1, -9.3])
# Define a mask (apply floor only where mask is True)
mask = np.array([True, False, True, False, True])
# Compute floor values where mask is True
result = np.floor(arr, where=mask)
# Print the results
print("Floored values with mask:", result)
Output:
Floored values with mask: [ 3. 0.5 -5. 1.5 -10. ]

The floor values are computed only for elements where mask=True
. The other values remain unchanged.