In this post, we will see how we can calculate the n-th Discrete Difference in Python. We will use the numpy.diff() and, MaskedArray.diff() methods in Python to calculate the first difference and for higher values, the diff() method is called recursively to perform the same set of tasks.
Calculate the n-th Discrete Difference using the numpy.diff()Â
The numpy.diff(arr[, n[, axis]]) function is used when we calculate the n-th order discrete difference along the given axis.
Example 1: Demonstrate 1st-order difference.
Import the required module and create the NumPy array then calculate the difference by its first order/second order/ third order. Since, it is a first-order difference the value is obtained by the following operation, [1,2,3,4,5]-[0,1,2,3,4] = [1,1,1,1].
Python3
# Example program to show first order differenceimport numpy as gÂ
a = g.array([1, 2, 3, 4, 5])Â
print("First order difference : ", g.diff(a)) |
Output:
First order difference : [1 1 1 1]
 Example 2: Demonstrate 2nd and 3rd order difference
Here, the input array is [1,2,6,4,9]. The first-order difference can be given by [1,2,6,4,9]-[0,1,2,6,4] = [1,1,4,-2,5] Â whereas, the second order difference can be given by a[i+1]-a[i]= [ 1-1, 4-1, -2-4, 5-(-2)]= [0,3,-6,7], third order difference can be given by [-6-3,7+6] = [-9,13].
Python3
import numpy as gÂ
a = g.array([1, 2, 6, 4, 9])Â # forming the numPy arrayÂ
# second order difference calculationprint("Second order difference : ", g.diff(a, n=2))Â
# Program to demonstrate 3rd order differenceb = g.array([1, 2, 6, 4, 9])Â
# third order difference calculationprint("Third order difference : ", g.diff(b, n=3)) |
Output:
Second order difference : [ 3 -6 7] Third order difference : [-9 13]
Example 3: Code to demonstrate how to use NumPy diff() inÂ
By default, the axis is taken as -1. when we set axis = 0 , it means to calculate the differences along the row using the numpy.diff() method and when we set axis =1 , it means to calculate the difference along the column using the numpy.diff() method.
Python3
# importing the moduleimport numpy as gÂ
# creating the numpy arraya = g.array([[1, 2, 6, 4, 9], [8, 4, 7, 5, 3]])Â
# Difference along axis# axis=0 means column wise# difference is calculatedprint("Given axis = 0 ", g.diff(a, axis=0))Â
# axis=1 means row wise# difference is calculatedprint("Given axis = 1 ", g.diff(a, axis=1)) |
Output:
Given axis = 0 [[ 7 2 1 1 -6]] Given axis = 1 [[ 1 4 -2 5] [-4 3 -2 -2]]
Calculate the n-th Discrete Difference using the MaskedArray.diff() method
In this approach, we will be using the masked array approach using the MaskedArray.diff() method in Python. The first-order difference is given by out[i]=arr[i+1] – arr[i] along the given axis. If we have to calculate higher differences, we are using diff recursively.Â
Python3
import numpy as gimport numpy.ma as mÂ
a = g.array([[1, 2, 6, 4, 9], [8, 4, 7, 5, 3]])temp = m.masked_array(a, mask=Â Â Â Â Â Â Â Â Â Â Â Â Â Â [[1, 0, 0, 0, 0], [0, 1, 0, 0, 0]])print(g.diff(temp)) |
Output:
[[-- 4 -2 5] [-- -- -2 -2]]
