# Calculate the n-th discrete difference along specific axis in Numpy

NumpyServer Side ProgrammingProgramming

#### Python Data Science basics with Numpy, Pandas and Matplotlib

Most Popular

63 Lectures 6 hours

#### Data Analysis using NumPy and Pandas

19 Lectures 8 hours

#### Numpy with Python

Most Popular

12 Lectures 3 hours

To calculate the n-th discrete difference along the given axis, use the MaskedArray.diff() method in Python Numpy. The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively −

• The axis is set using the "axis" parameter

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

The function returns the n-th differences. The shape of the output is the same as a except along axis where the dimension is smaller by n. The type of the output is the same as the type of the difference between any two elements of a. This is the same as the type of a in most cases. A notable exception is datetime64, which results in a timedelta64 output array.

The prepend, append parameter are the values to prepend or append to a along axis prior to performing the difference. Scalar values are expanded to arrays with length 1 in the direction of axis and the shape of the input array in along all other axes. Otherwise the dimension and shape must match a except along axis.

## Steps

At first, import the required library −

import numpy as np
import numpy.ma as ma

Create an array with int elements using the numpy.array() method −

arr = np.array([[65, 68, 81], [93, 33, 76], [73, 88, 51], [62, 45, 67]])
print("Array...
", arr)

Create a masked array and mask some of them as invalid −

maskArr = ma.masked_array(arr, mask =[[1, 0, 0], [ 0, 0, 0], [0, 1, 0], [0, 0, 0]])
print("

Get the type of the masked array −

print("

Get the dimensions of the Masked Array −

print("

Get the shape of the Masked Array −

print("

Get the number of elements of the Masked Array −

print("
Number of elements in the Masked Array...

To calculate the n-th discrete difference along the given axis, use the MaskedArray.diff() method. The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively. The axis is set using the "axis" parameter. The axis is the axis along which the difference is taken, default is the last axis:

print("
Result..

## Example

import numpy as np
import numpy.ma as ma

# Create an array with int elements using the numpy.array() method
arr = np.array([[65, 68, 81], [93, 33, 76], [73, 88, 51], [62, 45, 67]])
print("Array...
", arr) # Create a masked array and mask some of them as invalid maskArr = ma.masked_array(arr, mask =[[1, 0, 0], [ 0, 0, 0], [0, 1, 0], [0, 0, 0]]) print("
",maskArr.shape) # Get the number of elements of the Masked Array print("
Number of elements in the Masked Array...
",maskArr.size) # To calculate the n-th discrete difference along the given axis, use the MaskedArray.diff() method in Python Nump # The first difference is given by out[i] = a[i+1] - a[i] along the given axis, higher differences are calculated by using diff recursively. # The axis is set using the "axis" parameter # The axis is the axis along which the difference is taken, default is the last axis. print("
Result..

Array...
[[65 68 81]
[93 33 76]
[73 88 51]
[62 45 67]]

[[-- 68 81]
[93 33 76]
[73 -- 51]
[62 45 67]]

int64

2