Curriculum
Course: NumPy
Login

Curriculum

NumPy

Text lesson

ufunc Differences

Differences

A discrete difference is obtained by subtracting successive elements in an array. For instance, the discrete difference of [1, 2, 3, 4] is [1, 1, 1]. This can be calculated using the diff() function.

Example

Calculate the discrete difference for the following array:

import numpy as np

arr = np.array([1015255])

newarr = np.diff(arr)

print(newarr)

We can repeat the discrete difference operation by specifying a parameter nn. For example, with the array [1, 2, 3, 4] and n=2n=2, the first discrete difference is [1, 1, 1]. Applying the operation again gives [0, 0].

Example

Calculate the discrete difference of the following array two times:

import numpy as np

arr = np.array([1015255])

newarr = np.diff(arr, n=2)

print(newarr)