What is the numpy.floor() function in NumPy?

Overview

The numpy.floor() function in NumPy is used to return the floor values of each element in an input array x such that for each of the elements of x, the floor values are less than or equal to it.

Syntax

numpy.floor(x, /, out=None, *, where=True)

Parameter value

The numpy.floor() function takes the following parameter values:

  • x (required): This represents the input array.
  • out (optional): This represents the location where the result is stored.
  • where (optional): This is the condition over which the input is being broadcast. At a given location where this condition is True, the resulting array will be set to the ufunc result. Otherwise, the resulting array will retain its original value.
  • **kwargs (optional): This represents other keyword arguments.

Return value

The numpy.floor() function returns an array containing the floor value of each element in the input array.

Example

import numpy as np
# creating an array
x = np.array([1.5, 10.3, 6.6, 0.3, 180.05])
# obtaining the floor values
myarray = np.floor(x)
print(x)
print(myarray)

Explanation

  • Line 1: We import the numpy module.
  • Line 4: We create an array x using the array() method.
  • Line 7: We implement the np.floor() function on the array. The result is assigned to a variable myarray.
  • Line 9: We print the input array x.
  • Line 10: We print the variable myarray.

Free Resources