Gato Marbré
Gato Marbré

Reputation: 43

numpy.diff with parameter n=2 produces strange result

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 !

Upvotes: 3

Views: 1175

Answers (2)

Carlo
Carlo

Reputation: 1578

"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]

Upvotes: 4

Riccardo Bucco
Riccardo Bucco

Reputation: 15364

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

Upvotes: 4

Related Questions