The np.floor_divide() function divides two arrays of the same size. The floor_divide() function returns the largest integer smaller or equal to the division of the inputs. It is equivalent to the division operator( // ) and pairs with Python.
For example, if we have two arrays, arr1 and arr2, then floor_divide will divide the values of arr2 by the values of arr1, but we will get a floor result.
This floor is equal to the python // operator pair with the remainder operator (%). The simplified equation is b = a % b + b * (a // b) up to round off. We can divide array elements by any scalar also.
Syntax
numpy.floor_divide(arr1, arr2, out=None)
Parameters
This function takes mainly two parameters:
- arr1: This is the input array that acts like a dividend.
- arr2: This is an input array that acts as a divisor.
- out: This is an optional field. A place the result will be stored in. If given, the shape to which the inputs broadcast must be in. If a freshly-allocated array is returned unless received or None. A tuple (possible as a keyword argument only) must have a length equal to the output number.
Return Value
The floor_divide() function returns the floor(x1/x2) where x1 and x2 are scalar.
Programming Example
Program to show the working of floor_divide() when both inputs are array.
# importing the numpy module import numpy as np # First Parameter arr_A = np.array([8, 18, 28, 34]) # Shape of first array, arr_A is: print("Shape of first array, arr_A is: ", arr_A.shape) # Second Parameter arr_B = np.array([2, 4, 5, 4]) # Shape of second array, arr_B is: print("Shape of second array, arr_B is:", arr_B.shape) out = np.floor_divide(arr_A, arr_B) print("Output obtained is: ", out) # Shape of output array, out is: print("Shape of output array, out is: ", out.shape)
Output
Shape of first array, arr_A is: (4,) Shape of second array, arr_B is: (4,) Output obtained is: [4 4 5 8] Shape of output array, out is: (4,)
Explanation
In this program, we have taken two numpy arrays named arr_A and arr_B consisting of different array elements; we have passed these two arrays as parameters inside the np.floor_divide() method; first parameter, arr_A acts as the dividend whose elements need to be divided and arr_B acts as the divisor.
The elements of arr_A are divided into element-wise fashion by the elements of arr_B.
The result of the floor_division() function is stored in a variable named out, which is the numpy array of the same shape as the input arrays and contains the quotient values obtained after division.
When the divisor is a scalar
# importing the numpy module import numpy as np # First Parameter arr_A = np.array([8, 18, 28, 34]) print("Elements in arr_A are: ", arr_A) # Second Parameter is a scalar value scal_val = 6 print("Scalar value is: ", scal_val) out = np.floor_divide(arr_A, scal_val) print("Output obtained is: ", out)
Output
Elements in arr_A are: [ 8 18 28 34] Scalar value is: 6 Output obtained is: [1 3 4 5]
Explanation
In the program, we have taken a numpy input array named arr_A, which performs the function of dividend and a scalar value, i.e., 6, which will act as the divisor; we have passed arr_A as the first parameter and the scalar value as the second parameter inside np.floor_divide() method, the scalar value will be broadcasted into an array of the same shape as arr_A.
Then the division is performed element-wise as usual.
The result of the division is stored in the variable named out, which is a numpy array of the same shape as an input array and contains the quotient values obtained after division.
That’s it for this tutorial.