What is the numpy.prod() Method

Numpy.prod() method “returns the product of array elements over a given axis.” If no axis is specified, the function calculates the product of all elements in the array.


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


arr1: It is a parameter interpreted as array_like, acting as the input data.

axis:  [Optional parameter] It can be an int, a tuple of ints, or None. This parameter specifies the axes along which the product is to be performed. The product is calculated from the last to the first axis for the negative axis. The product is performed on all the specified for a tuple of ints.

dtype: [Optional parameter] It specifies the type of the returned array. It also sets the accumulator in which the elements of the input array arr1 are to be multiplied. Unless arr1 has an integer dtype of less precision than the default platform integer, the dtype of arr1 is used by default. If arr1 is signed, then dtype is a platform integer or an unsigned integer of the same precision if arr1 is unsigned.

out: (ndarray) [Optional parameter]: It specifies an alternate output array where the resulting product will be placed. This must have the same shape as the expected output.

keepdims: (bool) [Optional parameter] If set to true, the reduced axes are left as dimensions with size one in the result. It is done to make the resulting broadcast properly against the array taken as input.

initial: (scalar) [Optional parameter]: It is the starting value for the product.

where: (array_like of bool) [Optional parameter]: These are the elements to be included in the product.

Return Value

The product of an array of elements over a given axis. Returns array reference to out if specified.

Numpy prod


  1. If the input array is blank, then this method returns the neutral element: 1

Example 1: How to Use numpy.prod() Method

import numpy as np

arr1 = [5, 6]
arr2 = np.prod(arr1)



Example 2: Using a 2D array

import numpy as np

arr1 = [[4,5],[1,3]]
arr2 = np.prod(arr1)



Example 3: Passing an empty array

Passing an empty array to the prod() function will return 1 as an output. The following code demonstrates the case where an empty array is passed as an input array.

import numpy as np

arr1 = []
arr2 = np.prod(arr1)



That’s it.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.