NumPy prod()

The numpy.prod() function calculates the product of elements in an array, either across the entire array or along a specified axis.

Syntax

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

Parameters

ParameterTypeDescription
aarray_likeInput data whose elements will be multiplied.
axisNone, int, or tuple of ints, optionalAxis or axes along which to compute the product. Default (None) computes the product of all elements.
dtypedtype, optionalThe desired data type of the output array.
outndarray, optionalAlternative output array where the result is stored. Must have the same shape as the expected output.
keepdimsbool, optionalIf True, retains reduced dimensions with size one.
initialscalar, optionalStarting value for the product calculation. Default is 1.
wherearray_like of bool, optionalDefines which elements to include in the product calculation.

Return Value

Returns an array with the product of elements along the specified axis. If out is provided, the result is stored there.


Examples

1. Computing the Product of All Elements in an Array

This example calculates the product of all elements in a 1D array.

</>
Copy
import numpy as np

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

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

# Print the result
print("Product of all elements:", result)

Output:

Product of all elements: 24

2. Computing the Product Along an Axis

We compute the product along different axes in a 2D array.

</>
Copy
import numpy as np

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

# Compute product along axis 0 (columns)
product_axis0 = np.prod(arr, axis=0)

# Compute product along axis 1 (rows)
product_axis1 = np.prod(arr, axis=1)

# Print the results
print("Product along axis 0 (columns):", product_axis0)
print("Product along axis 1 (rows):", product_axis1)

Output:

Product along axis 0 (columns): [ 4 10 18]
Product along axis 1 (rows): [ 6 120]

3. Using the out Parameter

Using an output array to store results instead of creating a new array.

</>
Copy
import numpy as np

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

# Create an output array with the same shape
output_array = np.empty(shape=())

# Compute product and store result in output_array
np.prod(arr, out=output_array)

# Print the results
print("Stored product result:", output_array)

Output:

Stored product result: 24.0

4. Using the initial Parameter

Setting an initial value for the product calculation.

</>
Copy
import numpy as np

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

# Compute product with an initial value of 10
result = np.prod(arr, initial=10)

# Print the result
print("Product with initial value 10:", result)

Output:

Product with initial value 10: 240

5. Using the where Parameter

Computing the product only for selected elements using a condition.

</>
Copy
import numpy as np

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

# Define a mask (compute product only for selected elements)
mask = np.array([True, False, True, False])

# Compute product where mask is True
result = np.prod(arr, where=mask)

# Print the result
print("Product with mask:", result)

Output:

Product with mask: 3

The product is computed only for elements where mask=True, ignoring the other values.