NumPy floor()

The floor() function rounds down each element in an array to the nearest smallest integer.


import numpy as np

array1 = np.array([1.9, 2.2, 3.1, 4.3])

# round down each element in array1 to nearest smallest integer result = np.floor(array1)
print(result) # Output: [1. 2. 3. 4.]

floor() Syntax

The syntax of floor() is:

numpy.floor(array, out=None)

floor() Arguments

The floor() function takes following arguments:

  • array - the input array whose elements are rounded down to the nearest integer.
  • out (optional) - the output array where the result is stored

floor() Return Value

The floor() function returns an array that contains the rounded-down values of each element in the input array.

Example 1: Use floor() with 2D Array

import numpy as np

# create a 2-D array
array1 = np.array([[1.2, 2.7, 3.5],
                                [4.9, 5.1, 6.8]])

# round down each element in array1 result = np.floor(array1)


[[1. 2. 3.]
 [4. 5. 6.]]

Here, we have used the floor() function to round down each element in array1.

The value 1.2 is rounded down to 1, the value 2.7 is rounded down to 2, and so on.

Note: The floor() function returns an array with the same data type as the input array, and the resulting values are floating-point numbers representing the rounded down values.

Example 2: Create Different Output Array to Store Result

import numpy as np

# create an array
array1 = np.array([1.2, 2.7, 3.5, 4.9])

# create an empty array with the same shape as array1
result = np.zeros_like(array1)

# store the result of floor() in out_array np.floor(array1, out=result)


[1. 2. 3. 4.]

Here, the floor() function is used with the out parameter set to result. This ensures that the result of applying floor() function is stored in result.