Home > other >  numpy.diff with parameter n=2 produces strange result
numpy.diff with parameter n=2 produces strange result

Time:03-29

I'm having a hard time understanding the behaviour of np.diff when n>1

The documentation gives the following example :

x = np.array([1, 2, 4, 7, 0])
np.diff(x)
array([ 1,  2,  3, -7])
np.diff(x, n=2)
array([  1,   1, -10])

It seems from the first example that we are substracting each number by the previous one (x[i 1]-x[i]) and all results make sense.

The second time the function is called, with n=2, it seems that we're doing x[i 2]-x[i 1]-x[i] and the two first numbers (1 and 1) in the resulting array make sense but I am surprised the last number is not -11 (0 -7 -4) but -10.

Looking in the documentation I found this explaination

The first difference is given by out[i] = a[i 1] - a[i] along the given axis, higher differences are calculated by using diff recursively.

I fail to understand this 'recursively' so I'd be glad if someone had a clearer explanation !

CodePudding user response:

np.diff(x, n=2) is the same as np.diff(np.diff(x)) (that's what "recursively" means in this case).

CodePudding user response:

"Recursively" in this case simply means it's performing the same operation multiple times, each time on the array resulting from the previous step. So:

x = np.array([1, 2, 4, 7, 0])
output = np.diff(x)

produces

output = [2-1, 4-2, 7-4, 0-7] = [1, 2, 3, -7]

If you use n=2, it simply does the same thing 2 times:

output = np.diff(x, n=2)
# first step, you won't see this result
output = [2-1, 4-2, 7-4, 0-7] = [1, 2, 3, -7]
# and then again (this will be your actual output)
output = [2-1, 3-2, -7-3] = [1, 1, -10]
  • Related