NumPy cumsum records a running total at every position along an array axis

A final sum describes a whole sequence, while cumulative sums retain the intermediate totals. NumPy cumsum adds entries progressively along a chosen axis. Academia Master Trader presents this English lesson for readers in Algeria, using constructed arrays to explain running totals, axis direction and the shape produced when no axis is supplied.

Every output includes the current entry and those before it

After import numpy as np, define v = np.array([3,5,-2,8]). The call np.cumsum(v) returns [3,8,6,14]. The first total is three. Adding five gives eight, adding negative two gives six and adding eight gives fourteen.

The third total is lower than the second because the next entry is negative. Cumulative does not mean that every total must increase. It means that the calculation includes progressively more entries from the same ordered sequence.

The output has four entries, matching the vector's length. Each result belongs to the corresponding input position. A simple sum would return fourteen alone, losing the intermediate totals that cumsum preserves.

NumPy running totals

An axis defines which sequence is accumulated

Define m = np.array([[2,4,7],[3,6,8]]). The call np.cumsum(m,axis=1) returns [[2,6,13],[3,9,17]], with shape (2,3). Each row has its own running total across columns.

The call np.cumsum(m,axis=0) returns [[2,4,7],[5,10,15]]. Here each column has its own running total down the rows. The first row stays unchanged, and the second row combines corresponding entries from both input rows.

Both operations preserve this matrix's shape because an axis was supplied. They do not reduce that axis to a single final total. Keep the axis next to the result when explaining which entries contributed to an intermediate sum.

Omitting the axis accumulates a flattened sequence

The call np.cumsum(m) returns [2,6,13,16,22,30], with shape (6,). The input is treated as the sequence [2,4,7,3,6,8]. Accumulation continues from the final entry of the first row into the first entry of the second row.

This differs from separately accumulating each row. The fourth total is sixteen rather than three because it includes the thirteen already accumulated from the first row. Omitting axis therefore changes both the output layout and the sequence of additions.

By default, cumsum returns a new result rather than replacing the input. This lets the original entries remain available beside the running totals. Preserving that pair is useful when explaining why a particular total changed from the previous position.

The accumulator dtype affects numeric behaviour

The optional dtype argument controls the accumulator and returned array type. For example, np.cumsum(v,dtype=np.float64) returns [3.0,8.0,6.0,14.0]. Choose a numeric type that fits the intended range and precision.

Integer arithmetic can overflow without raising an error. Floating point arithmetic can also accumulate rounding differences, so the final cumulative total need not exactly equal a separate sum computed through another summation routine. These are numeric representation issues rather than changes to the positional meaning of cumsum.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the accumulation axis, expected shape and numeric type beside each running total.