NumPy diff measures consecutive changes along an axis rather than returning the original levels

A sequence of levels and a sequence of changes describe different things. The level tells you the value at one position. A consecutive difference tells you how much the next value differs from the current one. Academia Master Trader explains NumPy diff in English for readers in Moldova, using small arrays whose arithmetic can be followed directly. These are constructed numerical examples, not market observations or evidence of financial performance.

Start with the direction of subtraction

After import numpy as np, define x = np.array([4,9,7,13]). The call np.diff(x) returns [5,-2,6]. The first result is nine minus four. The second is seven minus nine. The third is thirteen minus seven. Each result uses the later entry minus the earlier entry, not the reverse.

Four original entries create three adjacent pairs, so the output has length three. The first difference does not supply a change before the first original entry, because no earlier entry was provided. A shorter result is therefore expected. It is not a missing piece that should automatically be filled with zero.

Keep changes distinct from levels

The output five belongs to the pair four and nine; it is not a replacement level for four. Likewise, minus two describes the move from nine to seven. If you store differences beside their source sequence, choose and explain an alignment. For example, you might associate each change with the later entry of its pair. NumPy returns an array of numbers, while your explanation supplies that interpretation.

NumPy diff

Read the diagram in two stages

The figure places the original four entries in a gold row and the three first differences underneath. Read consecutive pairs from the original row and subtract in the stated direction. Then read the final row, which contains differences between consecutive first differences. Keeping these rows separate makes the meaning of each number visible without confusing a level with a change.

The first difference can be positive, negative or zero. A positive result means the next numerical value is larger. A negative result means it is smaller. Zero means the two adjacent entries are equal. Those descriptions concern the supplied values only; they do not predict the next value or explain why a sequence changed.

A second difference repeats the operation

The call np.diff(x,n=2) returns [-7,8]. Start from the first differences [5,-2,6]. Minus two minus five gives minus seven; six minus minus two gives eight. This operation does not subtract every second original entry. It applies consecutive differencing twice.

The output now has length two. With a sequence long enough for the requested order, each additional round removes one position along the selected axis. Write down the intermediate differences when explaining a higher order result. The arithmetic is easier to inspect when the transformation remains visible.

Choose the axis explicitly for a matrix

Define a = np.array([[4,9,7],[6,8,12]]). The call np.diff(a,axis=1) returns [[5,-2],[2,4]]. Each row is treated separately, and the shape changes from (2,3) to (2,2). By contrast, np.diff(a,axis=0) returns [[2,-1,5]], subtracting the first row from the second at matching columns. Its shape is (1,3).

The default axis is the last axis, so naming it explicitly can make an explanation clearer. Also choose a suitable data type: unsigned integer subtraction can wrap around instead of representing a negative value. For ordinary signed arithmetic, use an appropriate signed type before differencing. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the input, subtraction direction, order, axis and resulting shape. Those checks keep the numerical change distinct from the original values and from any interpretation you later add.