Numpy floor() function – Python NumPy floor() Function

NumPy floor() Function:

Numpy floor() function: The floor() function of the NumPy module returns the floor value of the given input array/data. The floor of the scalar x is the largest integer “i”, such that i <= x.

Syntax:

numpy.floor(x, out=None)

Parameters

x: This is required. It is an array (array-like) having elements for which the floor values are calculated.

out: This is optional. It is the location where the result will be saved. It must have a shape that the inputs broadcast to if it is provided. If None or not given, a newly allocated array is returned.

Return Value: 

The floor value of each element of x is returned.

NumPy floor() Function in Python

Example1

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the floor() function of the numpy module to get the floor values of the given array elements
  • Store it in another variable.
  • Print the floor values of the given array elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([20.3, 5.5, 8.8, 30.076, 12.123])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the above given array as an argument to the floor() function of the 
# numpy module to get the floor values of the given array elements
# Store it in another variable.
floor_vals = np.floor(gvn_arry)
# Print the floor values of the given array elements
print("The floor values of the given array elements:")
print(floor_vals)

Output:

The above given array is:
[20.3 5.5 8.8 30.076 12.123]
The floor values of the given array elements:
[20. 5. 8. 30. 12.]

Example2

np floor: Here, we give the array which includes the elements with negative values.

For example, Let the number given = -1.7

The floor value for -1.7 = -2

When we calculate the floor value for a given negative number, like -1.7, will be -2.

Because -1 is a higher number than -1.7, and -2 is a smaller number than -1.7

Approach:

  • Import numpy module using the import keyword.
  • Pass some random list of negative values as an argument to the array() function to create an array.
  • Store it in a variable.
  • Print the above-given array.
  • Pass the above-given array as an argument to the ceil() function of the numpy module to get the ceiling values of the given array elements with negative values.
  • Store it in another variable.
  • Print the ceiling values of the given array elements.
  • The Exit of the Program.

Below is the implementation:

# Import numpy module using the import keyword
import numpy as np
# Pass some random list of negative values as an argument to the array() function to
# create an array. 
# Store it in a variable.
gvn_arry = np.array([-20.3, -1.5, -2.8, -3, -12.6])           
# Print the above given array.
print("The above given array is:")
print(gvn_arry)
# Pass the above given array as an argument to the floor() function of the 
# numpy module to get the floor values of the given array elements
# Store it in another variable.
floor_vals = np.floor(gvn_arry)
# Print the floor values of the given array elements
print("The floor values of the given array elements:")
print(floor_vals)

Output:

The above given array is:
[-20.3 -1.5 -2.8 -3. -12.6]
The floor values of the given array elements:
[-21. -2. -3. -3. -13.]