NumPy diff calculates consecutive differences rather than percentage changes or distances

A difference describes a subtraction in a particular order. NumPy diff calculates consecutive differences along a chosen array axis. For the first difference, each output entry is the next input entry minus the current input entry. The order matters: reversing the subtraction would change its sign.

Academia Master Trader presents this lesson for English-language study in Saint Kitts and Nevis. The examples use invented numbers for array study, not market prices, trading signals or financial results. The purpose is to connect each output entry with the pair of input positions that produced it, before assigning any wider meaning to the result.

Follow one neighbouring pair at a time

Let x be [3,8,6,13]. The expression np.diff(x) returns [5,-2,7]. The first calculation is 8-3=5, the second is 6-8=-2 and the third is 13-6=7. Four input entries provide three neighbouring pairs, so the output contains three entries.

The result is not a list of distances that are always positive. The middle difference is negative because the next value, 6, is smaller than the current value, 8. Keeping the subtraction order visible explains that sign without changing the calculation into an absolute difference.

Original diagram connecting four input entries to their three consecutive differences with NumPy diff

Keep subtraction separate from percentage change

The value 5 in the first output position is a difference in the same numerical units as the input values. The function has subtracted 3 from 8. It has not divided by a starting value, multiplied by one hundred or supplied a percentage. Those would be separate calculations requiring their own definitions.

Likewise, an ordinary difference is not automatically a rate per unit of time. If input positions correspond to observations, the timing between them matters when interpreting a rate. NumPy diff applies the subtraction rule to the supplied positions; its output alone does not establish equal observation intervals or a time unit.

Count pairs before reading the output shape

Along a nonempty sequence, a first difference uses adjacent pairs and reduces the length by one when no boundary values are added. For the four-entry x, the result has length three. There is no output entry corresponding to a pair before the first input position because no earlier value was supplied.

When aligning differences with labels, describe which pair each label represents. Assigning the first output to the second input position can be useful in some presentations, but that is a labelling choice to explain. The calculation itself links two input positions, not a new independent observation at an unspecified location.

Apply a second difference to the first result

The parameter n sets how many times the differencing operation is applied. For the same x, the first result is [5,-2,7]. Applying the rule again gives [-7,9], because -2-5=-7 and 7-(-2)=9. This is np.diff(x,n=2).

A second difference does not compare every input entry with the value two positions ahead using one subtraction. It calculates differences of differences. Writing the intermediate sequence makes that meaning clear and provides a direct way to check the signs. Each stage uses the result of the previous stage.

Choose an axis for a matrix

Let a contain rows [2,5,9] and [8,10,15]. With the default last axis, np.diff(a) calculates differences across each row. The output rows are [3,4] and [2,5]. The row count remains two, while the column count changes from three to two.

For np.diff(a,axis=0), the subtraction runs between neighbouring rows instead. The output is one row, [6,5,6], obtained by subtracting the first row from the second column by column. The shape is (1,3). The same input supports different questions depending on which axis is selected.

Explain any supplied boundary value

The prepend argument supplies values before differencing; append supplies values after the input. For example, np.diff(x,prepend=0) gives [3,5,-2,7]. The added zero creates an extra first pair, so the first difference is 3-0=3. The remaining differences match the original three-pair calculation.

That initial zero is an explicit construction choice, not a recovered earlier observation. State why it was used. Adding a boundary value can change the output length, but it also changes the question answered by the first or final difference. Preserving length is not, by itself, an explanation of the added pair.

Read the data type as part of the calculation

These examples use ordinary signed numerical values. NumPy's documentation notes that unsigned integer differences remain unsigned. A subtraction that would be negative in signed arithmetic can therefore have a different representation. When negative differences are needed, choose a suitable signed type before applying the operation.

Continue learning with Academia Master Trader at academiamastertrader.com.br. Aprenda opens the educational material. Name the axis, write the next-minus-current subtraction for each pair and keep any boundary choice visible. That process explains the differences without treating them as percentages, rates or independent observations.