Python numpy.floor()
function is used to get the floor values of the input array elements. The NumPy floor()
function takes two main parameters and returns the floor value of each array element with a float data type. The floor value of the scalar x
is the largest integer y
, such that y<=x
.
In simple words, the floor value is always less than equal to the given value. In this article, I will explain how to use the Numpy floor() function with examples.
1. Quick Examples of Python NumPy floor() Function
If you are in a hurry, below are some quick examples of how to use Python NumPy floor() function.
# Below are the quick examples
# Example 1: Use numpy.floor() function to get single floor value
arr = np.array([7.8])
arr2 = np.floor(arr)
# Example 2: Use numpy.floor() function
arr = np.array([-2.9, -4.7, -0.6, 0.8, 4.1, 9.7, 8.0])
arr2 = np.floor(arr)
# Example 3: Use numpy.floor() function
arr = np.array([[0.8, 4.1, 9.7],[ 8.0, 5 ,6]])
arr2 = np.floor(arr)
2. Syntax of Python floor()
Following is the syntax of the numpy.floor() function.
# Syntax of Python numpy.floor()
numpy.floor(arr [, out]) = ufunc ‘floor’)
2.1 Parameters of floor()
arr
– Input arrayout
– It is ndarray, None, or tuple of ndarray and None, optional. Out will be the location where the result is to be stored. If provided, it must have a shape that the inputs broadcast to. If not provided or None, a freshly-allocated array is returned.
2.2 Return Value of floor()
This function returns an array that contains floor values of input array elements.
3. Usage of NumPy floor() Function
The np.floor()
is a mathematical NumPy library function that returns the floor value of the array element with a float data type. It verifies the value of the input variable, it must be a real number and assume x
and rounds the variable in a downwards manner to the nearest integer, and finally returns the before nearest integers as floor values. If a value of x
is an integer, it just returns the x
value as-is.
It varies from another function ceil() which is used to return the variable rounded upwards. Below I have covered some examples in order to understand the concept of floor()
.
4 Get Single Floor Value of 1-D Array
Create a NumPy array with a float element using numpy.array()
function. Then, I will apply the floor function to the input array.
import numpy as np
# Create single floor value
arr = np.array([7.8])
# Use numpy.floor() function to get single floor value
arr2 = np.floor(arr)
print(arr2)
# Output
# [7.]
From the above code, we got a floor value of the input array named arr
. That means 7.8 is our float element and its floor value is 7. (i.e before the nearest integer)
5 Get the Multiple Floor Values of 1-D Array
Using numpy.floor()
function we can also get the floor values of the input array with multiple elements. For example,
# Create an 1D input array
# Use numpy.floor() function
arr = np.array([0.8, 4.1, 9.7, 8.0, 5 ,6])
arr2 = np.floor(arr)
print(arr2)
# Output
# [0. 4. 9. 8. 5. 6.]
From the above code floor value for 0.8 is 0
and 6 is 6
, so we always pick the before nearest integer value.
6. Get Floor for Negative Integers
Let’s check the float values for negative numbers. To make it simple, I have used the same number from the above examples but with a negative. Notice the output and how it changes for negative numbers.
# Create an 1D input array
# Use floor(0 function
arr = np.array([-0.8, -4.1, -9.7, -8.0, -5, -6])
arr2 = np.floor(arr)
print(arr2)
# Output
# [ -1. -5. -10. -8. -5. -6.]
7. Use NumPy floor() on 2-Dimensional (Multi Dimension)
Finally, Let’s use the floor() methods for 2-dimensional arrays. Note that syntax doesn’t changes for 1-D or 2-D.
# Create 2-D array
arr = np.array([[0.8, 4.1, 9.7],[ 8.0, 5 ,6]])
# Use numpy.floor() function
arr2 = np.floor(arr)
print(arr2)
# Output :
# [[0. 4. 9.]
# [8. 5. 6.]]
Conclusion
In this article, I have explained how to use Python numpy.floor()
function, and using this how to calculate the floor values of all the array elements with examples.
Happy Learning!!
Related Articles
- How to get values from NumPy Array by Index?
- How to Slice NumPy Array?
- Python NumPy Array Reshape
- How to Get NumPy Array Shape?
- Get NumPy Array Length
- NumPy Ceil Function Example
- Python NumPy ceil() Function
- Python NumPy hstack Function
- Python NumPy Interpolate Function