What Is Numpy Diff? Along With Examples

Hello geeks and welcome to today’s article, we will discuss NumPy diff. Along with it, we will cover its syntax, different parameters, and also look at a couple of examples. But at first, let us try to understand it in general terms. Numpy is a mathematical module of python which provides a function called diff.

We can calculate the nth order discrete difference along with the given axis using Numpy Diff. The first-order difference is calculated by out[i]=arr[i+1]-arr[i]. To calculate the higher-order difference, we use the diff in a loop. Now we will look at its syntax followed by its parameters and then examples.

Syntax Of Numpy Diff

Given below is the general syntax of NumPy diff

numpy.diff(an=1axis=-1prepend=<no value>append=<no value>)

Here we can see that the syntax has several parameters, some optional and some not. In the next section, we will cover the parameters in-depth and look at each parameter’s importance.

Parameters Of Numpy Diff

As quite clear from the syntax there is a total of 5 parameters associated with it. We will cover each one in detail here

a : array_like

It represents the input array and is a mandatory parameter.

n : int

An optional parameter responsible for the number of times values is differenced. If no value is provided, it assumes the value to be 0, and the output is the same as the input.

axis : int

The axis along which the difference is to be taken. By default, it is assumed as the last axis.

Prepend, Append: array_like

An optional parameter, responsible for the values to be append or prepend “A” long an axis before performing differences. Scalar values are expanded to arrays with length 1 in the direction of the axis. Otherwise, the shape and dimension must match “A” except along the x-axis.

Examples Of Numpy Diff

Now lets us look at a couple of examples of NumPy diff

import numpy as ppool
arr=ppool.arr([1,2,3,4,5])
print("Input array  : ", arr) 
print("First order difference  : ", ppool.diff(arr, n=1)) 
print("Second order difference : ", ppool.diff(arr, n = 2)) 
print("Third order difference  : ", ppool.diff(arr, n = 3)) 

Output:

Input array  :  [1 2 3 4 5]
First order difference  :  [1 1 1 1]
Second order difference :  [0 0 0]
Third order difference  :  [0 0]

Explanation

In the above example, we can see the usage of NumPy diff. Here we can see that we have defined an array. In the first step, we have not defined the value of n. So by default, it has assumed it to be =0 and returned the input matrix. For the second case, we have defined n=1 so we get [1,1,1,1]. we can understand it as initial matrix[1,2,3,4,5]-[0,1,2,3,4]=[1,1,1,1]. Similarly for the next step [1,1,1,1]-[0,1,1,1]=[0,0,0]. The process continues in a loop until we reach our desired number of n is achieved.

What is Difference between Numpy Diff and Gradient?

Diff calculates the differences of matrix slices. This difference is calculated with a slice of dimension n. As a result, it returns the matrix of length smaller by n. Whereas, the gradient will produce an array of gradients with preserving the array length. So, if you want to numerical derivate of two arrays, you can use diff method provided you maintain the diff of the same length.

Must Read

One More Example NUMPY DIFF

import numpy as ppool  
  
arr = ppool.array([[1, 2, 3, 4], [5, 6, 7, 8]]) 
   
print("Input array  : ", arr) 
print("Difference when axis is 0 : ", ppool.diff(arr, axis = 0)) 
print("Difference when axis is 1 : ", ppool.diff(arr, axis = 1))

Output:

Input array  :  [[1 2 3 4]
 [5 6 7 8]]
Difference when axis is 0 :  [[4 4 4 4]]
Difference when axis is 1 :  [[1 1 1]
 [1 1 1]]

Explanation

In the above example, we see that instead of n, the operation is performed along the axis. We also know if the axis is not defined, it assumes the last axis for operating.

CONCLUSION

In this article, we covered NumPy diff along with its syntax and parameters. Apart from that, we also looked at a couple of examples that involved operations based on different parameters. Finally, we can conclude that NumPy diff is used to calculate the nth discrete difference along the given curve. I hope that this article was able to clear your doubts. If you have any unsolved doubts, feel free to write them below in the comment section. You can also refer to other articles related to Numpy here.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments