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
Parameter | Type | Description |
---|---|---|
a | array_like | Input data whose elements will be multiplied. |
axis | None, int, or tuple of ints, optional | Axis or axes along which to compute the product. Default (None ) computes the product of all elements. |
dtype | dtype, optional | The desired data type of the output array. |
out | ndarray, optional | Alternative output array where the result is stored. Must have the same shape as the expected output. |
keepdims | bool, optional | If True , retains reduced dimensions with size one. |
initial | scalar, optional | Starting value for the product calculation. Default is 1 . |
where | array_like of bool, optional | Defines 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.