NumPy ndarray.cumprod()
The numpy.ndarray.cumprod()
method computes the cumulative product of elements in an array.
It can operate across the entire array or along a specified axis, returning an array of the same shape with the cumulative product at each position.
Syntax
ndarray.cumprod(axis=None, dtype=None, out=None)
Parameters
Parameter | Type | Description |
---|---|---|
axis | None, int, optional | Axis along which the cumulative product is computed. If None , the array is flattened and processed. |
dtype | dtype, optional | The type of the returned array. Useful for preventing overflow in integer arrays. |
out | ndarray, optional | Alternative output array for storing the result. Must have the same shape as the expected output. |
Return Value
Returns an ndarray
of the same shape as the input, containing the cumulative product of elements along the specified axis.
Examples
1. Computing Cumulative Product of a 1D Array
This example computes the cumulative product of a one-dimensional NumPy array.
import numpy as np
# Creating a 1D array
arr = np.array([1, 2, 3, 4, 5])
# Computing the cumulative product of the array
cumprod_result = arr.cumprod()
# Printing the result
print("Cumulative product of the array:", cumprod_result)
Output:
Cumulative product of the array: [ 1 2 6 24 120]
Each element in the output array represents the product of all previous elements up to that position.
2. Using the axis
Parameter in ndarray.cumprod()
Computing the cumulative product along a specific axis in a 2D array.
import numpy as np
# Creating a 2D array
arr = np.array([[1, 2, 3],
[4, 5, 6]])
# Cumulative product along axis 0 (column-wise)
cumprod_axis0 = arr.cumprod(axis=0)
print("Cumulative product along axis 0:\n", cumprod_axis0)
# Cumulative product along axis 1 (row-wise)
cumprod_axis1 = arr.cumprod(axis=1)
print("Cumulative product along axis 1:\n", cumprod_axis1)
Output:
Cumulative product along axis 0:
[[ 1 2 3]
[ 4 10 18]]
Cumulative product along axis 1:
[[ 1 2 6]
[ 4 20 120]]
For axis=0
, each element accumulates the product from the elements above it.
For axis=1
, each element accumulates the product from the elements before it in the row.
3. Using dtype
to Prevent Overflow
Changing the data type of the output to prevent integer overflow when working with large numbers.
import numpy as np
# Creating an integer array with large values
arr = np.array([1000, 2000, 3000], dtype=np.int32)
# Computing cumulative product with default dtype (may overflow)
cumprod_default = arr.cumprod()
# Computing cumulative product with float dtype to prevent overflow
cumprod_float = arr.cumprod(dtype=np.float64)
print("Cumulative product with default dtype:", cumprod_default)
print("Cumulative product with float dtype:", cumprod_float)
Output:
Cumulative product with default dtype: [ 1000 2000000 6000000000]
Cumulative product with float dtype: [1.e+03 2.e+06 6.e+09]
Using dtype=np.float64
ensures numerical stability and prevents overflow when working with large numbers.
4. Storing the Result in an out
Array
Using the out
parameter to store the cumulative product result in a preallocated array.
import numpy as np
# Creating an array
arr = np.array([1, 2, 3, 4])
# Creating an output array of the same shape and dtype
out_array = np.empty_like(arr)
# Computing cumulative product and storing the result in out_array
arr.cumprod(out=out_array)
print("Cumulative product stored in output array:", out_array)
Output:
Cumulative product stored in output array: [ 1 2 6 24]
The result is stored in out_array
instead of creating a new array, optimizing memory usage.