# Numpy diff – Calculate the n-th discrete difference along the given axis.

In this article we implement `numpy diff` which is a function of the NumPy module in python. NumPy is an array-processing package that provides a high-performance multidimensional array object, and tools for working with these arrays. And we implement NumPy diff to calculate the nth discrete difference along the given axis.

Also read: Numpy ediff1d – The differences between consecutive elements of an array

## What is NumPy Diff?

`numpy.diff` is a function of the NumPy module provided by python. It is utilized for calculating the nth discrete difference along the given axis. If ‘x’ is the input array then the first difference is calculated by:

• `out[i]=x[i+1]-a[i]`

We can calculate higher differences by recursively applying the diff function.

### Syntax of Numpy diff

```numpy.diff(a , n=, axis=)
```

### Parameters

• a [array_like]: input array
• n [ int, optional ]: the number of times the values are differenced.
• axis [ int, optional ]: defines the axis along which operation to be carried out.

### Return value

• An array of the nth discrete difference.

## Quick Intro to Axis in array

A 2-dimensional array has two corresponding axes

### 1. axis 0: running vertically downwards across rows

```import numpy as np
arr=np.array([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]])
print(arr)
x=arr.sum(axis=0)
```

Output:

```[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[12 15 18 2
```

### 2. axis 1: running horizontally across columns

```import numpy as np
arr=np.array([[ 0, 1, 2, 3],[ 4, 5, 6, 7],[ 8, 9, 10, 11]])
print(arr)
x=arr.sum(axis=1)
```

Output:

```[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[12 15 18 21]
```

## Implementing Numpy.diff to find the difference between two values

To calculate the nth discrete difference we use the `out[i]=x[i+1]-a[i]` formula

When n=1, Let’s take the first row of our example 2D array

```out[1]=x[1+1]-a[1]    :    20 - 10 =10
out[2]=x[2+1]-a[2]    :    30 - 20 =10
out[3]=x[3+1]-a[3]    :    40 - 30=10
```

thus the result is [10 10 10]

When n=2, We will use the previously obtained array(in n=1) for getting the result

```out[1]=x[1+1]-a[1]  :  10 - 10 = 0
out[2]=x[2+1]-a[2]  :  10 - 10 = 0
```

thus the result is [ 0 0 ]

### Examples of Using Numpy.diff()

Importing numpy and declaring an array.

```import numpy as np
arr = np.array([[10,20,30,40],[50,60,70,80],[90,100,110,np.nan]])
```

Displaying the array of details

```print("Our Array...\n",arr)
print("shape of array: \n",arr.shape)
print("length of array: \n",len(arr))
print("\nDatatype of our Array object...\n",arr.dtype)
```

Output :

```Our Array…
[[ 10. 20. 30. 40.]
[ 50. 60. 70. 80.]
[ 90. 100. 110. nan]]
shape of array:
(3, 4)
length of arr
3
Datatype of our Array object…
float64
```

### Example 1: NumPy diff of row ie axis 0

```print("\nDiscrete difference with axis =0..\n",np.diff(arr, axis = 0))
```

This returns an array of discrete differences along axis 0

Output :

```Discrete difference with axis =0..
[[40. 40. 40. 40.]
[40. 40. 40. nan]]
```

### Example 2: NumPy diff of column ie axis =1

```print("\nDiscrete difference with axis =1 ..\n",np.diff(arr, axis = 1))
```

Calculating discrete difference along axis 1

Output :

```Discrete difference with axis =1 ..
[[10. 10. 10.]
[10. 10. 10.]
[10. 10. nan]]
```

### Example 3: 1st order diff of 2D array with axis =1

```print("\nDiscrete 1st order difference..\n",np.diff(arr, axis = 1))
```

The number of times we want an array to differ is calculated by assigning an n value. We differ the array once in this example along axis 1.

Output :

```Discrete 1st order difference and axis = 1 ..
[[10. 10. 10.]
[10. 10. 10.]
[10. 10. nan]]
```

### Example 4: 2nd order diff of 2D array with axis = 1

```print("\nDiscrete 2nd order difference and axis = 1..\n", np.diff(arr, axis = 1,n=2))
```

Calculating the discrete value for the second time along axis 1.

Output :

```Discrete 2nd order difference and axis = 1..
[[ 0. 0.]
[ 0. 0.]
[ 0. nan]]
```

### Example 5: 3rd order diff of 2D array with axis = 1

```print("\nDiscrete 3rd order difference and axis = 1..\n",np.diff(arr, axis = 1,n=3))
```

Output :

```Discrete 3rd order difference and axis = 1..
[[ 0.]
[ 0.]
[nan]]
```

### Example 6: 1st order diff of 2D array with axis =0

Here we calculate values along axis 0.

```print("\nDiscrete 1st order difference and axis = 0 ..\n",np.diff(arr, axis = 0))
```

Output :

```Discrete 1st order difference and axis = 0 ..
[[40. 40. 40. 40.]
[40. 40. 40. nan]]
```

Example 7 : 2nd order diff of 2D array with axis = 0

```print("\nDiscrete 2nd order difference and axis = 0..\n",np.diff(arr, axis = 0,n=2))
```

Output :

```Discrete 2nd order difference and axis = 0..
[[ 0. 0. 0. nan]]
```

Example 8 : 3rd order diff of 2D array with axis = 0

```print("\nDiscrete 3rd order difference and axis = 0..\n",np.diff(arr, axis = 0,n=3))
```

The output array is null as we have performed a recursive function enough on the elements to reach null values.

Output :

```Discrete 3rd order difference and axis = 0..
[]
```

## Conclusion

In this article, we understand the working of NumPy.diff function of the NumPy module in Python which is used to find the difference between the array values horizontally or vertically. We implement NumPy.diff with different nth and axis values via 2D array examples.