NumPy prod()

The prod() function calculates the product of array elements along a specified axis or across all axes.

Example

import numpy as np

array1 = np.array([1, 2, 3, 4, 5])

# use prod() to calculate product of array1 elements result = np.prod(array1)
print(result) # Output : 15

prod() Syntax

The syntax of prod() is:

numpy.prod(array, axis = None, dtype = None, out = None, keepdims = <no value>)

prod() Arguments

The prod() function takes following arguments:

  • array - the input array
  • axis (optional) - the axis along which the product is calculated
  • dtype (optional) - the data type of the returned output
  • out (optional) - the output array where the result will be stored
  • keepdims (optional) - whether to preserve the input array's dimension (bool)

prod() Return Value

The prod() function returns the product of array elements.


Example 1: prod() With 2-D Array

The axis argument defines how we can find the product of elements in a 2-D array.

  • If axis = None, the array is flattened and the product of the flattened array is returned.
  • If axis = 0, the product is calculated column-wise.
  • If axis = 1, the product is calculated row-wise.
import numpy as np

# create a 2D array
arr = np.array([[1, 2, 3],
                [4, 5, 6],
                [7, 8, 9]])

# calculate the product along different axes result_none = np.prod(arr, axis=None) result_cols = np.prod(arr, axis=0) result_rows = np.prod(arr, axis=1)
print("Product of all elements (axis=None):", result_none) print("Product along columns (axis=0):", result_cols) print("Product along rows (axis=1):", result_rows)

Output

Product of all elements (axis=None): 362880
Product along columns (axis=0): [ 28  80 162]
Product along rows (axis=1): [  6 120 504]

Example 2: Use out to Store the Result in Desired Location

import numpy as np

array1 = np.array([[10, 17, 25], 
                 	    [15, 11, 22], 
                  	    [11, 19, 20]])

# create an empty array
array2= np.array([0, 0, 0])

# pass the 'out' argument to store the result in array2 np.prod(array1, axis = 0, out = array2)
print(array2)

Output

[ 1650  3553 11000]

Here, after specifying out=array2, the result of the product of array1 along axis=0 is stored in the array2 array.


Example 3: prod() With keepdims

When keepdims = True, the dimensions of the resulting array matches the dimension of an input array.

import numpy as np

array1 = np.array([[10, 17, 25], 
            		    [15, 11, 22]])
print('Dimensions of original array: ', array1.ndim)

result = np.prod(array1, axis = 1)
print('\n Without keepdims: \n', result) print('Dimensions of array: ', result.ndim)
# set keepdims to True to retain the dimension of the input array result = np.prod(array1, axis = 1, keepdims = True)
print('\n With keepdims: \n', result) print('Dimensions of array: ', result.ndim)

Output

Dimensions of original array:  2

 Without keepdims: 
[4250 3630]
Dimensions of array:  1

 With keepdims: 
 [[4250]
 [3630]]
Dimensions of array:  2

Without keepdims, the result is simply a one-dimensional array of indices.

With keepdims, the resulting array has the same number of dimensions as the input array.