Numpy Diff Implementation Using numpy.diff() method

Numpy Diff Implementation

Numpy diff calculates the n-th order discrete difference along the given axis. It calculates the first-order difference using the formulae result[n] = a[n+1] – a[n] .Here a denotes NumPy array. The second or higher-order differences are calculated recursively.

In this entire article, you will know how to implement NumPy diff with various examples.

But before moving to the coding examples let’s learn the syntax of the numpy.diff() method.

The syntax of numpy diff() method

numpy.diff(a, n=1, axis=-1)

a: Input array

n: Order of difference. The number of times values are differenced.

axis: The axis along which the difference is taken, default is the last axis.

The method returns the difference of array type along the axis.

 

NumPy diff  of 1D Array

In this section, you will learn to calculate the difference of 1 D Numpy array. Let’s create the array.

array_1d = np.array([10,20,30,40,50,60,70])

Just pass the array inside the diff() method.

np.diff(array_1d)

The above code will calculate the first-order difference. To calculate the 2,3, or n difference you have to pass the n =2,3,4,5, so on.

NumPy 2nd order diff

np.diff(array_1d,n=2)

3rd Order Difference

np.diff(array_1d,n=3)

Output

NumPy diff  of 1D Array
NumPy diff  of 1D Array

NumPy difference of 2D Array

The same diff() method calculates the difference of two or N-dimensional array. Here you can also calculate numpy difference along the axis that is row-wise or column-wise. Let’s create a Two-dimension NumPy array.

array_3x4 = np.array([[10,20,30,40],[50,60,70,80],[90,100,110,120]])

1st order diff

np.diff(array_3x4)

The above code will find the first-order difference column-wise. If you want to do calculation row-wise then you have to pass the axis value to 0.

NumPy diff of rows

np.diff(array_3x4,axis=0)

Just like you are finding the 2,3 or n order in one-dimensional array. You can also find it here bypassing the n=2,3,4 or so on.

Output

NumPy difference of 2D Array
NumPy difference of 2D Array

Other Examples

Numpy Difference for Time-Series Data

If You have time-series then you can also calculate the difference over it. Let’s create a time series array for date of datetime type.

data = np.arange('2020-11-01', '2020-11-16', dtype=np.datetime64)

Just pass the data inside the diff() method to get the output.

np.diff(data)

Output

Numpy Difference for Time-Series Data
Numpy Difference for Time-Series Data

Conclusion

These are examples for finding numpy difference for both one and two-dimensional array. The application of finding a difference is that you can find some useful patterns in the dataset that helps you do predict results. For example in stock markers.

Hope you have liked this tutorial. If you have any queries then you can contact us.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner