![]() |
VOOZH | about |
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().
24
Explanation: np.prod(arr) multiplies all elements of arr: 1 × 2 × 3 × 4 = 24.
numpy.prod(a, axis=None, dtype=None, out=None, keepdims=False)
Parameters:
Example 1: This example finds the product of all elements in a 1D array using np.prod().
24
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.
[ 2 12]
Explanation: axis=1 calculates the product across each row:
Example 3: This example shows the result when np.prod() is applied to an empty array.
1.0
Explanation: For an empty array, np.prod() returns 1.0, which is the neutral value for multiplication.