NumPy cumprod()

The numpy.cumprod() function computes the cumulative product of array elements along a given axis. If no axis is specified, the input array is flattened before computation.

Syntax

</>
Copy
numpy.cumprod(a, axis=None, dtype=None, out=None)

Parameters

ParameterTypeDescription
aarray_likeInput array whose cumulative product is to be computed.
axisint, optionalThe axis along which the cumulative product is computed. If None, the input is flattened.
dtypedtype, optionalSpecifies the data type of the returned array and accumulator. Defaults to the input array’s dtype unless it’s an integer with lower precision than the platform default.
outndarray, optionalAn alternative array where the result is stored. Must have the same shape as the expected output.

Return Value

Returns an ndarray with the cumulative product of the input array. If the out parameter is specified, a reference to that array is returned.


Examples

1. Computing the Cumulative Product of a 1D Array

In this example, we compute the cumulative product of a one-dimensional NumPy array.

</>
Copy
import numpy as np

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

# Compute the cumulative product
cumprod_result = np.cumprod(arr)

# Print the result
print("Original array:", arr)
print("Cumulative product:", cumprod_result)

Output:

Original array: [1 2 3 4 5]
Cumulative product: [  1   2   6  24 120]

2. Using the axis Parameter

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

</>
Copy
import numpy as np

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

# Compute the cumulative product along axis 0 (column-wise)
cumprod_axis0 = np.cumprod(arr, axis=0)

# Compute the cumulative product along axis 1 (row-wise)
cumprod_axis1 = np.cumprod(arr, axis=1)

# Print results
print("Original array:\n", arr)
print("Cumulative product along axis 0:\n", cumprod_axis0)
print("Cumulative product along axis 1:\n", cumprod_axis1)

Output:

Original array:
 [[1 2 3]
 [4 5 6]]
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

Setting a specific data type for the output.

</>
Copy
import numpy as np

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

# Compute the cumulative product with float dtype
cumprod_float = np.cumprod(arr, dtype=float)

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

Output:

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

4. Using the out Parameter

Using an output array to store the cumulative product results.

</>
Copy
import numpy as np

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

# Create an output array with the same shape
out_array = np.empty_like(arr)

# Compute the cumulative product and store it in out_array
np.cumprod(arr, out=out_array)

# Print the results
print("Computed cumulative product:", out_array)

Output:

Computed cumulative product: [ 1  2  6 24]