NumPy diff subtracts adjacent entries along a selected array axis

An ordered sequence can be described through the changes between neighbouring entries. NumPy diff calculates discrete differences along an array axis. Academia Master Trader presents this English lesson for readers in Sudan, using constructed arrays to explain subtraction order, shorter output dimensions and the distinction between a second difference and a wider gap.

The next entry minus the current entry defines a first difference

After import numpy as np, define v = np.array([2,6,13,23]). The call np.diff(v) returns [4,7,10]. These values come from six minus two, thirteen minus six and twenty three minus thirteen.

Each output position describes one neighbouring pair. Four input entries provide three such pairs, so the result has shape (3,). Diff does not compare every entry with the first one. It also does not retain the first input value as an initial output entry.

A decreasing step produces a negative difference for a signed numeric array. For example, np.diff(np.array([9,4,7])) returns [-5,3]. The order of subtraction matters: each later entry is reduced by the entry immediately before it.

NumPy adjacent differences

Higher differences repeat the operation on the previous result

The call np.diff(v,n=2) returns [3,3]. First NumPy obtains [4,7,10], then it takes adjacent differences of that intermediate result. Seven minus four and ten minus seven both equal three.

Setting n to two does not mean comparing entries two positions apart in the original vector. A higher difference is a repeated operation. Writing the intermediate result makes this distinction visible and explains why the selected output dimension becomes shorter again.

With n=0, NumPy returns the input as it is. For ordinary positive orders within the available axis length, each round removes one position along that axis when no extra entries are supplied.

The axis determines which entries are neighbours

Define m = np.array([[2,6,13],[5,12,20]]). The call np.diff(m,axis=1) returns [[4,7],[7,8]], with shape (2,2). Subtraction occurs across columns within each row.

The call np.diff(m,axis=0) returns [[3,6,7]], with shape (1,3). It subtracts the first row from the second, matching corresponding columns. For this matrix, omitting axis uses the final axis, so it agrees with axis one.

Prepended values create an additional neighbouring pair

The call np.diff(v,prepend=0) returns [2,4,7,10]. NumPy places zero before the original sequence before calculating differences. The first output is therefore two minus zero, not a copied value added after the calculation.

Choose that additional value for a stated reason rather than using it only to force a desired output length. It changes the sequence being compared and therefore changes the meaning of the first difference.

Array dtype also matters. Unsigned integers cannot represent ordinary negative results, so a decreasing step can wrap within that dtype. Convert to a suitable signed type before differencing when negative changes are intended. Boolean inputs instead report whether neighbouring values differ, rather than producing signed numeric changes.

Continue learning with Academia Master Trader at academiamastertrader.com.br and in Aprenda. Record the axis, difference order and any added endpoint before interpreting the output.