Numpy divide by scalar – Python Numpy divide() Function

Numpy divide() Function:

Numpy divide by scalar: The divide function in Numpy calculates the division of the two arrays. It calculates the element-by-element split between the two arrays, say l1 and l2. The numpy. divide() function is a universal function, which means it has numerous options that can be used to optimize its performance based on the algorithm’s characteristics.

Syntax:

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

Parameters:

l1 and l2: (Required)

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

out:

Numpy element wise division: 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: 

Python element wise division: The divide function returns the result of the division of l1 and l2. The nd-array scalar divide() function can be used. It is determined by the l1 and l2 variables. Numpy is used if l1 and l2 are both scalar. A scalar value will be returned by divide(). Otherwise, an nd-array will be returned.

NumPy 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 divide() function.
  • Pass the first array and some random number to divide() function of NumPy module and print the result.
  • Here it divides the second argument for each element of the array.
  • Print the result array.
  • Pass the first array and second array to divide() function of NumPy module and print the result.
  • Here it 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([[30,60],[90,120],[150,180]])
# Create some sample arrays of different shapes to test the 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 divide() function of NumPy module and print the result.
# Here it divides the second argument for each element of the array.
print('Dividing random number say 10 to the first array : ')
print('Dividing 10 to first array gives :\n',np.divide(arry1, 10))
# Print the result array.
# Pass the first array and second array to divide() function of NumPy module and print the result.
# Here it divides the second array elements for the first array element.
# Print the result array.
print('Dividing first array and second array : ')
print(np.divide(arry1, arry2))
# Similarly, test it with other arrays of different shapes.
print('Dividing first array and third array : ')
print(np.divide(arry1, arry3))
print('Dividing first array and fourth array : ')
print(np.divide(arry1, arry4))

Output:

Dividing random number say 10 to the first array : 
Dividing 10 to first array gives :
[[ 3. 6.]
[ 9. 12.]
[15. 18.]]
Dividing first array and second array : 
[[ 3. 3.]
[ 9. 6.]
[15. 9.]]
Dividing first array and third array : 
[[0.3 0.6 ]
[0.45 0.6 ]
[0.5 0.6 ]]
Dividing first array and fourth array : 
[[0.54545455 0.92307692]
[1.2 1.41176471]
[1.57894737 1.71428571]]