numpy.diff() in Python with examples

This tutorial will help you learn about how to use numpy.diff() in Python.

numpy.diff() is one of the most widely used functions in Python and basically, it is used to calculate the difference between two consecutive values of a NumPy array. The output of the function numpy.diff() comes out in the form of out[i]=arr[i+1]-arr[i]

Example 1:

This is a very basic example to understand the numpy.diff() function.

# import the library
import numpy as np
# create the array
array=np.array([4,2,5,3,9,1,7,0,9])
# difference between two consecutive elements of this array
# out[i]=arr[i+1]-arr[i]
new_array=np.diff(array)
# print the new array
print(new_array)
Output:
[-2  3 -2  6 -8  6 -7  9]

In this example, we can clearly see that the difference between all the consecutive values of this array is given in the output section of this Python program. This is also known as the first order difference.

Example 2:

Now, if you want to calculate the difference between the consecutive values of a NumPy array multiple times, i.e… to calculate the difference between the consecutive values of the array made from the output of the first order difference. In this case, the first order difference array is [-2 3 -2 6 -8 6 -7 9].

Second order difference:

# import the library
import numpy as np
# create the array
array=np.array([4,2,5,3,9,1,7,0,9])
print("Original Array",array)
# difference between two consecutive elements of this array
# out[i]=arr[i+1]-arr[i]
First_order=np.diff(array)
print("First order difference: ",First_order)
# Second order difference
new_array=np.diff(array,n=2)
# print the new array
print("Second order difference: ",new_array)
Output:
Original Array [4 2 5 3 9 1 7 0 9]
First order difference:  [-2  3 -2  6 -8  6 -7  9]
Second order difference:  [  5  -5   8 -14  14 -13  16]

Third order difference:

# import the library
import numpy as np
# create the array
array=np.array([4,2,5,3,9,1,7,0,9])
print("Original Array",array)
# difference between two consecutive elements of this array
# out[i]=arr[i+1]-arr[i]
First_order=np.diff(array)
print("First order difference: ",First_order)
# Second order difference
new_array=np.diff(array,n=2)
# print the new array
print("Second order difference: ",new_array)
# Third order difference
print("Third order difference: ",np.diff(array,n=3))
Output:
Original Array [4 2 5 3 9 1 7 0 9]
First order difference:  [-2  3 -2  6 -8  6 -7  9]
Second order difference:  [  5  -5   8 -14  14 -13  16]
Third order difference:  [-10  13 -22  28 -27  29]

Example 3:

We can also calculate the difference between the elements of a 2-dimensional NumPy array using the code below.

# Import the library
import numpy as np
# create the array
array=np.array([[4,3,5,2,6],[2,1,8,9,4]])
# difference between the elements
difference=np.diff(array,axis=0)
# print the difference array
print(difference)
Output:
[[-2 -2  3  7 -2]]

Difference between the elements of the sub-arrays:

# Import the library
import numpy as np
# create the array
array=np.array([[4,3,5,2,6],[2,1,8,9,4]])
# difference between the elements of the sub-array
difference=np.diff(array,axis=1)
# print the difference array
print(difference)
Output:
[[-1  2 -3  4]
 [-1  7  1 -5]]

Leave a Reply

Your email address will not be published. Required fields are marked *