.cumprod()
Published Apr 28, 2025
Contribute to Docs
In NumPy, the .cumprod()
function computes the cumulative product of elements in an array along a specified axis. Each element in the result is the product of all preceding elements (including the current one) along the chosen axis.
Syntax
numpy.cumprod(a, axis=None, dtype=None, out=None)
Parameters:
a
: Input array for which the cumulative product is to be calculated.axis
(Optional): Axis along which the cumulative product is computed. Default isNone
, the input is flattened.dtype
(Optional): The data type of the output array. Defaults to the type of the input array.out
(Optional): Alternative output array to place the result. If provided, it must have the same shape as the expected output.
Return value:
Returns a NumPy array containing the cumulative product of elements along the specified axis.
Example
The following example demonstrates how to compute the cumulative product of elements using .cumprod()
:
import numpy as np# Create an arrayarray = np.array([1, 2, 3, 4, 5])# Compute the cumulative product of the arraycumulative_product = np.cumprod(array)# Print out the original arrayprint("Original Array:", array)# Print out the returned arrayprint("Cumulative Product:", cumulative_product)
The code produces the following output:
Original Array: [1 2 3 4 5]Cumulative Product: [ 1 2 6 24 120]
Codebyte Example
Run this interactive code example to see how .cumprod()
works for both 1D and 2D arrays with axis variation:
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn Python:NumPy on Codecademy
- Career path
Data Scientist: Machine Learning Specialist
Machine Learning Data Scientists solve problems at scale, make predictions, find patterns, and more! They use Python, SQL, and algorithms.Includes 27 CoursesWith Professional CertificationBeginner Friendly95 hours - Course
Learn Python 3
Learn the basics of Python 3.12, one of the most powerful, versatile, and in-demand programming languages today.With CertificateBeginner Friendly23 hours