Numpy.prod () in Python



numpy.prod () returns the product of array elements along a given axis. 
Syntax :

  numpy.prod (a, axis = None, dtype = None, out = None, keepdims =)   

parameters
a: array_like
This is the input. 
axis: none or int or a tuple of integers, it is optional
This is the axis or axes along which the product is produced. The default axis is None, it will calculate the product of all elements of the input array. If the axis is negative, it is counted from the last to the first axis. 
If the axis is a tuple of integers, the product is performed along all axes specified in the tuple, not along one axis or all axes, as before. 
dtype: dtype, optional
This is the type of the returned array as well as the accumulator that the elements are multiplied by. The d type for a is the default, unless a has an integer dtype with less precision than the platform`s default integer. In this case, if a is signed, then the platform integer is used, while if a is unsigned, then an unsigned integer of the same precision is used as the platform integer. 
out: ndarray, optional
Alternative output array for placing the result. It should have the same shape as the expected result, but the type of the output values ​​will be cast as needed. 
keepdims: bool, optional
If this parameter is set to True, axes that are downsized remain in the result as dimensions with size one. With this option, the result will translate correctly with respect to the input array.

Example 1

# Program Python illustrating
# working product ()

 

import numpy as geek 

array1 = [ 1 , 2 ]

  
# using the function

array2 = np.prod (array1)

 

 print ( "product" , array2)

Output:

 2.0 

Example 2
2d array

import numpy as geek 

array1 = [[ 1. , 2. ], [ 3. , 4. ]]

 
# function application

array2 = np.prod (array1)

  

print ( " product " , array2)

Output:

 24.0 

Example 3
The product of an empty array will be neutral element 1:

import numpy as geek 

array1 = []

 
# using the function

array2 = np.prod (array1)

 

print ( " product " , array2)

Exit :

 1 

Example 4
Specifying the axis along which we multiply

import numpy as geek 

array1 = [[ 1 , 2 ], [ 3 , 4 ]]

 
# applying the function

array2 = np.prod (array1, axis = 1  )

 

print ( " product " , array2)

Output:

 [2, 12] 

Example 5
If type x is unsigned, the output type will be an unsigned integer platform

import numpy as geek 

x = np.array ([ 1 , 2 , 3 ], dtype = np.uint8)

  
# function application

np .prod (x) .dtype = = np.uint

Output:

 True