# Python Numpy true_divide() Function

Numpy true_divide() Function:

The product of the two NumPy arrays is calculated using the NumPy multiply function. It calculates the element-by-element product of the two arrays, say l1 and l2. The numpy. multiply() 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.true_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:

True division of l1 and l2 (l1/l2) is returned by the true divide() function.

## NumPy true_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 true_divide() function.
• Pass the first array and some random number to true_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 true_divide() function of NumPy module and print the result.
• Here it truly 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 true_divide() function.
arry2 = np.array([[10,20]])
arry3 = np.array([,,])
arry4 = np.array([[55,65],[75,85],[95,105]])
# Pass the first array and some random number to true_divide() function of NumPy module and print the result.
# Here it divides the second argument for each element of the array.
print('True Dividing random number say 10 to the first array : ')
print('True Dividing 10 to first array gives :\n',np.true_divide(arry1, 10))
# Print the result array.
# Pass the first array and second array to true_divide() function of NumPy module and print the result.
# Here it true divides the second array elements for the first array element.
# Print the result array.
print('True Dividing first array and second array : ')
print(np.true_divide(arry1, arry2))
# Similarly, test it with other arrays of different shapes.
print('True Dividing first array and third array : ')
print(np.true_divide(arry1, arry3))
print('True Dividing first array and fourth array : ')
print(np.true_divide(arry1, arry4))

Output:

True Dividing random number say 10 to the first array :
True Dividing 10 to first array gives :
[[ 3.2 6.4]
[ 9.5 12.8]
[15. 18. ]]
True Dividing first array and second array :
[[ 3.2 3.2]
[ 9.5 6.4]
[15. 9. ]]
True Dividing first array and third array :
[[0.32 0.64 ]
[0.475 0.64 ]
[0.5 0.6 ]]
True Dividing first array and fourth array :
[[0.58181818 0.98461538]
[1.26666667 1.50588235]
[1.57894737 1.71428571]]