mindspore.numpy.diff

mindspore.numpy.diff(a, n=1, axis=- 1, prepend=None, append=None)[源代码]

Calculates the n-th discrete difference along the given axis.

The first difference is given by \(out[i] = a[i+1] - a[i]\) along the given axis, higher differences are calculated by using diff iteratively.

说明

Since zero-shaped Tensor is not supported in MindSpore, a value error is raised if an empty Tensor is encountered.

参数
  • a (Tensor) – Input tensor.

  • n (int, optional) – The number of times values are differenced. If zero, the input is returned as-is. Default: 1.

  • axis (int, optional) – The axis along which the difference is taken, default is the last axis. Default: -1.

  • prepend/append (Tensor, optional) – 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 axis. Otherwise the dimension and shape must match a except along axis. Default: None.

返回

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.

异常
Supported Platforms:

Ascend GPU CPU

样例

>>> import mindspore.numpy as np
>>> arr = np.array([1, 3, -1, 0, 4])
>>> print(np.diff(arr, n=2))
[-6  5  3]