NumPy cumprod keeps the intermediate products along a chosen array axis

Multiplying an entire sequence gives one final product. NumPy cumprod instead records a product at every position, preserving the stages of multiplication. Academia Master Trader presents this English lesson for readers in Morocco, using constructed arrays to explain cumulative products, independent axis sequences and the effect of a zero within a sequence.

Each output multiplies the current entry by the preceding product

After import numpy as np, define v = np.array([2,3,-1,4]). The call np.cumprod(v) returns [2,6,-6,-24]. The first output is two. Multiplying by three gives six, multiplying by negative one gives negative six and multiplying by four gives negative twenty-four.

The vector retains four positions. Each result includes all entries up to and including that position. The output is therefore different from a reduction that keeps only the final product.

Cumulative products need not grow or remain positive. A negative factor can change the sign, while a factor between zero and one can reduce the magnitude. The operation follows multiplication rather than imposing a direction on the results.

NumPy cumulative products

Specifying an axis creates independent accumulation sequences

Define m = np.array([[2,3,4],[5,2,3]]). The call np.cumprod(m,axis=1) returns [[2,6,24],[5,10,30]], with shape (2,3). Each row starts its own sequence and accumulates across its columns.

The call np.cumprod(m,axis=0) returns [[2,3,4],[10,6,12]]. Each column is accumulated down the rows. The first row provides the initial products, while the second row multiplies corresponding entries from both input rows.

Both results preserve the matrix shape. The chosen axis identifies the direction of accumulation, not an axis to discard. Naming the axis alongside an example helps readers distinguish the separate sequences within the same matrix.

Without an axis the matrix becomes one sequence

The call np.cumprod(m) returns [2,6,24,120,240,720], with shape (6,). Its accumulation order follows the flattened sequence [2,3,4,5,2,3].

The fourth result is one hundred and twenty because the product from the first row continues into the second row. It does not restart at five. Omitting axis therefore changes the output shape and the membership of each cumulative product.

Keep the original order visible when explaining the calculation. Reordering factors preserves the final exact product in ordinary arithmetic, but changes the intermediate products associated with individual positions. A cumulative result describes the supplied sequence, rather than an unordered collection of values.

A zero affects every later product in its sequence

For z = np.array([2,3,0,4]), np.cumprod(z) returns [2,6,0,0]. Once zero enters this finite integer sequence, multiplying subsequent entries keeps the running product at zero. Cumprod does not automatically restart after the zero.

The optional dtype argument controls the accumulator and output type. For example, np.cumprod(v,dtype=np.float64) returns [2.0,6.0,-6.0,-24.0]. Integer multiplication can overflow without raising an error, so choose a type suited to the intended numeric range. By default, the operation returns a new array and leaves the input available for comparison.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the axis, expected shape and numeric type before explaining the intermediate products.