NumPy cumulative_prod()

The numpy.cumulative_prod() function computes the cumulative product of elements along a specified axis in an array.

Syntax

</>
Copy
numpy.cumulative_prod(x, /, *, axis=None, dtype=None, out=None, include_initial=False)

Parameters

ParameterTypeDescription
xarray_likeInput array for which the cumulative product is computed.
axisint, optionalAxis along which the cumulative product is calculated. If None, it applies to a one-dimensional array. For multi-dimensional arrays, specifying an axis is required.
dtypedtype, optionalDefines the data type of the output array. If not specified, it defaults to the input dtype, or the platform’s default integer type if x has low-precision integer dtype.
outndarray, optionalSpecifies an output array where results will be stored. It must match the expected output shape.
include_initialbool, optionalIf True, includes an initial value of ones in the output, altering its shape. Defaults to False.

Return Value

Returns an array of the same shape as x (unless include_initial=True), containing the cumulative product of elements along the specified axis.


Examples

1. Computing Cumulative Product for a 1D Array

Here, we compute the cumulative product of elements in a one-dimensional array.

</>
Copy
import numpy as np

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

# Compute cumulative product
result = np.cumulative_prod(arr)

# Print the result
print("Cumulative product:", result)

Output:

Cumulative product: [ 1  2  6 24]

2. Computing Cumulative Product Along an Axis

For multi-dimensional arrays, we specify an axis to compute the cumulative product along rows or columns.

</>
Copy
import numpy as np

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

# Compute cumulative product along axis 0 (column-wise)
result_axis0 = np.cumulative_prod(arr, axis=0)

# Compute cumulative product along axis 1 (row-wise)
result_axis1 = np.cumulative_prod(arr, axis=1)

# Print the results
print("Cumulative product along axis 0:\n", result_axis0)
print("Cumulative product along axis 1:\n", result_axis1)

Output:

Cumulative product along axis 0:
 [[ 1  2  3]
 [ 4 10 18]]

Cumulative product along axis 1:
 [[ 1  2  6]
 [ 4 20 120]]

3. Using the dtype Parameter

Specifying a higher precision dtype to prevent overflow in integer arrays.

</>
Copy
import numpy as np

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

# Compute cumulative product with dtype set to float
result = np.cumulative_prod(arr, dtype=np.float64)

# Print the results
print("Cumulative product with float dtype:", result)

Output:

Cumulative product with float dtype: [ 2.  6. 24.]

4. Using the out Parameter

Storing the cumulative product result in a pre-allocated output array.

</>
Copy
import numpy as np

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

# Create an output array
out_arr = np.empty_like(arr)

# Compute cumulative product and store result in out_arr
np.cumulative_prod(arr, out=out_arr)

# Print the output array
print("Output array with cumulative product:", out_arr)

Output:

Output array with cumulative product: [ 1  2  6 24]

5. Using the include_initial Parameter

Including an initial value (ones) in the cumulative product computation.

</>
Copy
import numpy as np

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

# Compute cumulative product with initial value included
result = np.cumulative_prod(arr, include_initial=True)

# Print the result
print("Cumulative product with initial value:", result)

Output:

Cumulative product with initial value: [ 1  2  6 24]

With include_initial=True, an extra 1 is added at the start of the output array.