NumPy diff calculates differences between adjacent entries along a chosen axis

A sequence of values and the differences between those values answer different questions. NumPy diff calculates the change from each entry to the next along a selected axis. Academia Master Trader explains the operation in English for readers in Kyrgyzstan. Constructed examples connect the subtraction rule, output length and axis choice without treating a difference as a percentage, a forecast or an independent measurement.

Subtract the earlier entry from the next

After import numpy as np, define a = np.array([3,7,6,12]). The call np.diff(a) returns [4,-1,6]. Seven minus three gives four. Six minus seven gives minus one. Twelve minus six gives six. The subtraction follows the supplied array order, not a separate chronological interpretation.

Four source entries produce three adjacent differences. Each output entry belongs to a pair of neighboring input positions. A positive result means that the later position has a larger value; a negative result means it has a smaller value. This describes the pair, not the reason for its change.

NumPy diff

A difference is not automatically a rate

The first result, four, is an absolute difference in the units of the input. It is not four percent. Computing a relative change would require a separately chosen denominator and a rule for cases where that denominator is zero. Likewise, a rate per hour requires information about the time between observations.

Diff does not inspect dates or infer equal time spacing from the order of an ordinary numeric vector. Keep any time coordinates with the source and state their meaning. If the array has been sorted by value rather than observation time, the neighboring pairs describe that sorted arrangement instead.

Higher order means differences of differences

The call np.diff(a,n=2) returns [-5,7]. It first forms [4,-1,6], then calculates minus one minus four and six minus minus one. These are second differences. They are not the changes between every second original entry, and they are not the first differences multiplied by two.

In this four entry example, one difference reduces the length to three and a second reduces it to two. The parameter n states how many times the differencing operation is applied. With n=0, the input is returned as it is. Keep the order visible when documenting a derived array.

The axis chooses the neighboring positions

Define m = np.array([[2,5,9],[4,10,15]]). The default call np.diff(m) returns [[3,4],[6,5]], because the default axis is the last one. Here that means moving across each row. The input shape (2,3) becomes (2,2).

By contrast, np.diff(m,axis=0) returns [[2,5,6]]. It subtracts the first row from the second at each column, producing shape (1,3). Neither result is more correct without knowing what rows and columns represent. Name the axis by its data meaning as well as its numeric label.

Boundary values and data types matter

The call np.diff(a,prepend=0) returns [3,4,-1,6]. Zero is added before the first input entry for the calculation, so the new first difference is three minus zero. The output has four entries, but its first pair includes an explicitly supplied boundary value rather than two original observations.

Unsigned integer arrays also need attention. An unsigned eight bit array containing [1,0] produces [255] under diff, rather than a signed minus one. Convert to a suitable signed type before subtraction when negative results are intended. Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the order, axis, difference order and any boundary value alongside the result.