• Post author:
  • Post category:NumPy / Python
  • Post last modified:March 27, 2024
  • Reading time:16 mins read
You are currently viewing Python NumPy floor() Function Examples

In NumPy, the numpy.floor() function is used to round elements of an array to the nearest integer, towards negative infinity. It returns the floor of input, element-wise. The result is an array with the same shape as the input array, but with each element rounded down to the nearest integer.

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 NumPy floor() Function

If you are in a hurry, below are some quick examples of how to use Python NumPy floor() function.


# Quick examples of numpy floor() function

# 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([0.8, 4.1, 9.7, 8.0, 5 ,6])
arr2 = np.floor(arr)

# Example 3: Use numpy.floor() 
# Get floor for negative integers
arr = np.array([-0.8, -4.1, -9.7, -8.0, -5, -6])
arr2 = np.floor(arr)

# Example 4: Use numpy floor() on 2-dimensional
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 – This is the array or value for which you want to find the floor. It can be a one-dimensional array, a multi-dimensional array, or a single numeric value.
  • out – 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 assumes x and rounds the variable in a downward 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.

The floor value of a single element in a NumPy array using numpy.floor() function. Creates a NumPy array arr containing a single element, which is 7.8. Applies numpy.floor() to round down the single element in the array. Prints the floor value.


# Import numpy
import numpy as np

# Create single floor value
arr = np.array([7.8])
print("Original array:",arr)

# Use numpy.floor() function 
# To get single floor value
arr2 = np.floor(arr)
print("Floor value:",arr2)

Yields below output.

numpy floor

In this case, numpy.floor() rounds down the single-element 7.8 to 7.0, and the resulting array arr2 contains the floor value.

5 Get the Multiple Floor Values of 1-D Array

If you want to obtain the floor values for all elements in a 1-dimensional NumPy array, you can directly apply numpy.floor() to the entire array. For instance, arr2 contains the floor values for each element in the original array arr. The numpy.floor() function is applied element-wise to round down each value in the array.


# Create an 1D input array
arr = np.array([0.8, 4.1, 9.7, 8.0, 5 ,6])
print("Original array:",arr)

# Use numpy.floor() function
arr2 = np.floor(arr)
print("Rounded down values:",arr2)

Yields below output.

numpy floor

From the above code floor value for 0.8 is 0 and 6 is 6 , so you always pick the before nearest integer value.

6. Get Floor for Negative Integers

To obtain the floor values for negative integers in a 1-dimensional NumPy array, you can use the numpy.floor() function in the same way as with positive numbers. The numpy.floor() function rounds each element of the array down to the nearest integer, towards negative infinity.

In the below example, the arr2 array contains the floor values for each element in the original array arr, including negative integers. The numpy.floor() function handles both positive and negative numbers, rounding towards negative infinity.


# Create an 1D input array
arr = np.array([-0.8, -4.1, -9.7, -8.0, -5, -6])
print("Original array:",arr)

# Use numpy.floor() 
# To round down each element
arr2 = np.floor(arr)
print("Rounded down values:",arr2)

# Output:
# Original array: [-0.8 -4.1 -9.7 -8.  -5.  -6. ]
# Rounded down values: [ -1.  -5. -10.  -8.  -5.  -6.]

7. Use NumPy floor() on 2-Dimensional (Multi Dimension)

When using numpy.floor() on a 2-dimensional array, the function will operate element-wise, rounding each element down to the nearest integer.

In the below example, numpy.floor() is applied element-wise to each element of the 2D array arr, rounding each element down to the nearest integer. The resulting array, arr2, has the same shape as the input array.


# Create 2-D array
arr = np.array([[0.8, 4.1, 9.7],[ 8.0, 5 ,6]])
print("Original array:\n",arr)

# Use numpy.floor() function
arr2 = np.floor(arr)
print("Rounded down values (2D):\n",arr2)

# Output : 
# Original array:
#  [[0.8 4.1 9.7]
#  [8.  5.  6. ]]
# Rounded down values (2D):
#  [[0. 4. 9.]
#  [8. 5. 6.]]

Frequently Asked Questions

What does the numpy.floor() function do?

The numpy.floor() function in Python, provided by the NumPy library, rounds each element of an array down to the nearest integer, towards negative infinity. The result is a new array with the same shape as the input array, where each element is the floor value of the corresponding element in the input array.

How is the numpy.floor() function used?

The numpy.floor() function is used to round the elements of an array down to the nearest integer, towards negative infinity. It operates element-wise on the input array, producing a new array with the same shape as the input, where each element is the floor value of the corresponding element in the input array.

Does numpy.floor() modify the original array?

The numpy.floor() function does not modify the original array in-place. It returns a new array with the rounded values. If you want to modify the original array in-place, you can use the // operator or the numpy.floor_divide() function.

Can numpy.floor() be applied to multi-dimensional arrays?

The numpy.floor() function can be applied to multi-dimensional arrays. When you apply it to a multi-dimensional array, it operates element-wise on each element of the array, rounding each element down to the nearest integer.

Are there alternatives to numpy.floor() for rounding towards negative infinity?

There are alternative ways to achieve rounding towards negative infinity in NumPy. Two common alternatives are using the numpy.floor_divide() function and the floor division operator //.

Can numpy.floor() be used with non-numeric arrays?

The numpy.floor() function is designed for numeric arrays, and attempting to use it with non-numeric arrays will result in an error. The function relies on the concept of rounding down to the nearest integer, which is meaningful only for numerical data.

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!!

References