Python NumPy floor_divide() Function

NumPy floor_divide() Function:

The floor_divide() function of the Numpy module returns the largest integer that is less or equal to the inputs’ division. It works in pairs with Python and is equal to the division operator (//).

The floor_divide() function in Numpy is used to divide two identical arrays. If we have two arrays, l1 and l2, the floor divide will divide l2 values by l1 values, but the result will be a floor. **

b = a % b + b * (a // b) up to round off is the simplified equation.

The floor is equal to the (//) operator in python pair with the remainder operator (%).

Any scalar can also be used to divide array items.

Syntax:

numpy.floor_divide(l1, l2, out=None)

Parameters:

l1 and l2: (Required)

Required. The arrays that will be divided must be specified. They must be broadcastable to a common shape if l1.shape!= l2.shape

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 of (l1/l2) is returned by the floor_divide() function of the NumPy module in which l1 and l2 are the two scalar values. 

NumPy floor_divide() Function in Python

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.
  • Create some sample arrays of different shapes to test the floor_divide() function.
  • Pass the first array and some random number to the floor_divide() function of the NumPy module and print the result.
  • Here it floor divides(//) the second argument for each element of the array.
  • Print the result array.
  • Pass the first array and second array to the floor_divide() function of the NumPy module and print the result.
  • Here it floor divides the second array elements for the first array element.
  • Print the result array.
  • Similarly, test it with other arrays of different shapes.
  • 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.
arry1 = np.array([[32,64],[95,128],[150,180]])
# Create some sample arrays of different shapes to test the floor_divide() function.
arry2 = np.array([[10,20]])
arry3 = np.array([[100],[200],[300]])
arry4 = np.array([[55,65],[75,85],[95,105]])
# Pass the first array and some random number to floor_divide() function of NumPy module and print the result.
# Here it floor divides(//) the second argument for each element of the array.
print('Floor Dividing random number say 10 to the first array : ')
print('Floor Dividing 10 to first array gives :\n',np.floor_divide(arry1, 10))
# Print the result array.
# Pass the first array and second array to floor_divide() function of NumPy module and print the result.
# Here it floor divides the second array elements for the first array element.
# Print the result array.
print('Floor Dividing first array and second array : ')
print(np.floor_divide(arry1, arry2))
# Similarly, test it with other arrays of different shapes.
print('Floor Dividing first array and third array : ')
print(np.floor_divide(arry1, arry3))
print('Floor Dividing first array and fourth array : ')
print(np.floor_divide(arry1, arry4))

Output:

Floor Dividing random number say 10 to the first array : 
Floor Dividing 10 to first array gives :
[[ 3 6]
 [ 9 12]
 [15 18]]
Floor Dividing first array and second array : 
[[ 3 3]
 [ 9 6]
 [15 9]]
Floor Dividing first array and third array : 
[[0 0]
 [0 0]
 [0 0]]
Floor Dividing first array and fourth array : 
[[0 0]
 [1 1]
 [1 1]]