下一主题

numpy.ediff1d

numpy.diff

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

计算沿给定轴的第n个离散差分。

The first difference is given by out[n] = a[n+1] - a[n] along the given axis, higher differences are calculated by using diff recursively.

参数:

a:array_like

输入数组

n
int,可选

差值的次数。

int,可选

采用差值的轴,默认值为最后一个轴。

返回:

diff:ndarray

第n个差异。除了,输出形状与a相同,其中尺寸小于n

例子

>>> x = np.array([1, 2, 4, 7, 0])
>>> np.diff(x)
array([ 1,  2,  3, -7])
>>> np.diff(x, n=2)
array([  1,   1, -10])
>>> 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]])