numpy.diff

numpy.diff(a, n=1, axis=-1)

Calculate the nth order discrete difference along given axis.

Parameters:

a : array_like

Input array

n : int, optional

The number of times values are differenced.

axis : int, optional

The axis along which the difference is taken.

Returns:

out : ndarray

The n order differences. The shape of the output is the same as a except along axis where the dimension is n less.

Examples

>>> x = np.array([0,1,3,9,5,10])
>>> np.diff(x)
array([ 1,  2,  6, -4,  5])
>>> np.diff(x,n=2)
array([  1,   4, -10,   9])
>>> x = np.array([[1,3,6,10],[0,5,6,8]])
>>> np.diff(x)
array([[2, 3, 4],
[5, 1, 2]])
>>> np.diff(x,axis=0)
array([[-1,  2,  0, -2]])

Previous topic

numpy.cumsum

Next topic

numpy.ediff1d

This Page

Quick search