Open In App

Calculate the n-th Discrete Difference in Python

Last Updated : 24 Jan, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 difference
import 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 calculation
print("Second order difference : ", g.diff(a, n=2))
 
# Program to demonstrate 3rd order difference
b = g.array([1, 2, 6, 4, 9])
 
# third order difference calculation
print("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 module
import numpy as g
 
# creating the numpy array
a = g.array([[1, 2, 6, 4, 9], [8, 4, 7, 5, 3]])
 
# Difference along axis
# axis=0 means column wise
# difference is calculated
print("Given axis = 0 ", g.diff(a, axis=0))
 
# axis=1 means row wise
# difference is calculated
print("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 g
import 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]]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads