NumPy ndarray.prod()

The numpy.ndarray.prod() method computes the product of array elements over a specified axis. It can calculate the product of all elements in an array or along a specific axis.

Syntax

</>
Copy
ndarray.prod(axis=None, dtype=None, out=None, keepdims=False, initial=1, where=True)

Parameters

ParameterTypeDescription
axisNone, int, or tuple of ints, optionalAxis or axes along which to compute the product. If None, it computes the product of the entire array.
dtypedtype, optionalThe desired data type of the output. If None, it defaults to the array’s type.
outndarray, optionalAlternative output array to store the result. Must have the same shape as expected output.
keepdimsbool, optionalIf True, the reduced dimensions are kept as size one, preserving the original dimensions for broadcasting.
initialscalar, optionalThe starting value for the product. Defaults to 1.
wherearray_like of bool, optionalElements to include in the product calculation.

Return Value

Returns a scalar if axis=None or an array of values if an axis is specified. The result is the product of all elements (or elements along the specified axis).


Examples

1. Computing the Product of All Elements in ndarray

In this example, we compute the product of all elements in a NumPy array.

</>
Copy
import numpy as np

# Creating an array
arr = np.array([1, 2, 3, 4])

# Computing the product of all elements
result = arr.prod()

# Printing the result
print(result)

Output:

24

The product of 1 × 2 × 3 × 4 results in 24.

2. Using the axis Parameter in ndarray.prod()

We compute the product along a specific axis in a 2D array.

</>
Copy
import numpy as np

# Creating a 2D array
arr = np.array([[1, 2], 
                [3, 4]])

# Computing the product along axis 0 (columns)
result_axis0 = arr.prod(axis=0)
print(result_axis0)

# Computing the product along axis 1 (rows)
result_axis1 = arr.prod(axis=1)
print(result_axis1)

Output:

[3 8]
[2 12]

For axis=0 (columns), the product is computed as:

  • Column 1: 1 × 3 = 3
  • Column 2: 2 × 4 = 8

For axis=1 (rows), the product is computed as:

  • Row 1: 1 × 2 = 2
  • Row 2: 3 × 4 = 12

3. Using keepdims=True in ndarray.prod()

We compute the product while retaining the dimensions.

</>
Copy
import numpy as np

# Creating a 2D array
arr = np.array([[2, 3],
                [4, 5]])

# Computing the product along axis 1 while keeping dimensions
result = arr.prod(axis=1, keepdims=True)

# Printing the result
print(result)

Output:

[[ 6]
 [20]]

The result retains the original shape, making it useful for broadcasting.

4. Using the initial Parameter in ndarray.prod()

The initial parameter sets a starting value for the product.

</>
Copy
import numpy as np

# Creating an array
arr = np.array([2, 3, 4])

# Computing the product with an initial value of 10
result = arr.prod(initial=10)

# Printing the result
print(result)

Output:

240

The calculation follows: 10 × 2 × 3 × 4 = 240.

5. Using the where Parameter in ndarray.prod()

The where parameter filters which elements to include in the product.

</>
Copy
import numpy as np

# Creating an array
arr = np.array([2, 3, 4])

# Creating a mask to exclude the second element
mask = np.array([True, False, True])

# Computing the product where the mask is True
result = arr.prod(where=mask)

# Printing the result
print(result)

Output:

8

Only the elements 2 × 4 are included in the product.