Hello geeks and welcome in 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.


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.


As quiet clear from the syntax there are total 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.


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

import numpy as ppool
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)) 


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]


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.

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))


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]]


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.


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.

5 1 vote
Article Rating
Notify of
Inline Feedbacks
View all comments