Numpy.prod() in python is used to compute the product of the array elements over a specified axis.
- numpy.prod() returns an array with the same shape as the input array, with the specified axis removed.
- If an output array is specified in the out parameter, a reference to out is returned.
SYNTAX of numpy.prod() :
numpy.prod(array, axis=None, dtype=None, out=None, keepdims=<novalue>)
- array – the input array
- axis – It is optional, the axis along which the product is calculated
- dtype – It is optional, the data type of the returned output
- out – It is optional, the output array where the result will be stored
- keepdims – It is optional, whether to preserve the input array’s dimension(It is boolean function)
EXAMPLES :
Let us discuss 2 Programs using numpy.prod() now
Program 1:
import numpy as np #importing the numpy package to use np.prod() and given its alias name as np arr= np.array([1,2,5]) #Creating an array using np.array() function print(np.prod(arr)) #Calculating product of the array elements using np.prod()
Output: 10
Program 2:
import numpy as np #importing numpy package to use numpy.prod() arr1=np.array([2,2,5],[5,8,8]) #Calculating product along the columns print(np.prod(arr1,axis =0)) #Calculating product along the rows print(np.prod(arr1,axis=1))
Output: [10 16 40][20 320]
- In the above example axis as 0 represents that product is calculated column wise and axis as 1 represents that product is calculated row wise.
Finally I would conclude that I have explained in detail about Numpy.prod() .