Numpy.prod() in Python (original) (raw)
Last Updated : 5 Jun, 2026
numpy.prod() is used to calculate the product of array elements. It can multiply all elements of an array or multiply elements along a specific axis in multi-dimensional arrays. It is commonly used in numerical computing when we need the multiplication result of multiple values stored in a NumPy array.
**Example: This example multiplies all elements of a NumPy array using np.prod().
Python `
import numpy as np arr = [1, 2, 3, 4] res = np.prod(arr) print(res)
`
**Explanation: np.prod(arr) multiplies all elements of arr: 1 × 2 × 3 × 4 = 24.
Syntax
numpy.prod(a, axis=None, dtype=None, out=None, keepdims=False)
**Parameters:
- **a: Input array whose elements are multiplied.
- **axis: Axis along which the product is calculated. By default, product of all elements is returned.
- **dtype: Data type of the returned result.
- **out: Alternative output array to store the result.
- **keepdims: If True, reduced axes are kept as dimensions with size 1.
Examples
**Example 1: This example finds the product of all elements in a 1D array using np.prod().
Python `
import numpy as np arr = np.array([2, 3, 4]) res = np.prod(arr) print(res)
`
**Explanation: np.prod(arr) multiplies all array elements: 2 × 3 × 4 = 24.
**Example 2: This example calculates the product of elements row-wise in a 2D array using axis=1.
Python `
import numpy as np arr = np.array([[1, 2], [3, 4]]) res = np.prod(arr, axis=1) print(res)
`
**Explanation: axis=1 calculates the product across each row:
- First row: 1 × 2 = 2
- Second row: 3 × 4 = 12
**Example 3: This example shows the result when np.prod() is applied to an empty array.
Python `
import numpy as np arr = [] res = np.prod(arr) print(res)
`
**Explanation: For an empty array, np.prod() returns 1.0, which is the neutral value for multiplication.