NumPy cumprod records successive products along an array in its supplied order
Multiplication can be recorded as a sequence of intermediate results rather than only as a final product. NumPy cumprod returns those successive products along a chosen axis. Academia Master Trader presents this English lesson for readers in Mongolia. Constructed numerical examples explain the accumulation rule, the effect of zero, and the difference between flattening a matrix and working along one of its axes.
Multiply through the current position
After import numpy as np, define a = np.array([2,3,4,2]). The call np.cumprod(a) returns [2,6,24,48]. The first output is two. The next output is two times three. The third includes four as well, producing twenty four, and the last includes the final two to produce forty eight.
There are four output entries, matching the number of input entries. Cumprod does not insert a separate initial one before the sequence. It does not add the values or multiply each entry by its numeric position. Each output includes every input from the beginning of this accumulation through the corresponding current position.

Intermediate products depend on order
Define b = np.array([4,2,2,3]). The call np.cumprod(b) returns [4,8,16,48]. These exact integer examples use the same factors and reach the same final product, but their intermediate results differ. The running sequence therefore describes the supplied arrangement, not only the collection of factors.
Keep that order explicit when factors represent successive stages in a calculation. A cumulative product does not decide whether an arrangement follows time, category or another coordinate. Nor does it establish that multiplying the supplied quantities is appropriate. Record what each factor means before assigning an interpretation to the returned numbers.
Zero carries through later finite products
For z = np.array([2,0,5,3]), the call np.cumprod(z) returns [2,0,0,0]. Once zero has entered this ordinary finite integer example, each later multiplication remains zero. Five and three are still present in the input, but the cumulative result no longer reveals their individual values.
A negative factor can change the sign of a nonzero running product. For example, np.cumprod([2,-3,4]) returns [2,-6,-24]. The result is a sequence of signed products, not absolute magnitudes. Keep the original factors if later work needs to distinguish the individual contributions that produced a total.
The default combines a flattened matrix
Define m = np.array([[2,3,2],[4,2,3]]). The default call np.cumprod(m) returns [2,6,12,48,96,288]. With no axis supplied, the input is flattened before accumulation. The result is a vector with six entries instead of a matrix shaped (2,3).
The first factor of the second row multiplies the product already reached at the end of the first row. Thus the fourth output is forty eight, not four. That default behavior is useful only when one continuous sequence across the flattened layout matches the intended calculation.
Choose an axis and accumulator type
The call np.cumprod(m,axis=1) returns [[2,6,12],[4,8,24]]. Each row accumulates independently. The call np.cumprod(m,axis=0) returns [[2,3,2],[8,6,6]], accumulating down each column instead. Both explicit axis results preserve the matrix shape, but they describe different groups of factors.
The dtype argument controls the output and accumulator type. Fixed width integer multiplication can overflow without raising an error, so choose a suitable type for the expected products. Changing the type does not decide the data meaning or guarantee exact arithmetic for every quantity. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. State the factor order, axis and numeric type with every cumulative product.









