numpy.diff() method is used to find the nth order of discrete difference along a specified axis.
Note: In Python, a list of lists can be used to create a two-dimensional (2D) array.
The syntax for the
numpy.diff() method is as follows:
numpy.diff(a, n=1, axis=-1)
a: This represents the input data.
n: This is an optional parameter. It denotes the number of times the difference value is calculated.
axis: This is an optional parameter. It denotes the axis over which the difference is calculated.
numpy.diff() method returns the nth order of discrete difference along a given axis.
The following code shows how to use the
numpy.diff() method for two-dimensional (2D) arrays.
# import numpy import numpy as np # create list x1 = [3,4,8] x2 = [2,6,5] # convert the lists to 2D array using np.array a = np.array([x1,x2]) # compute the n-th discrete order difference # and store the result in result result = np.diff(a, axis=0) print(result)
The following is the explanation for the code above:
np.array()method to convert the lists to a 2D array.
np.diff()to compute the nth discrete difference along
axis=0. The result is stored in a new variable called
View all Courses