np.diff函数
数组中a[n]-a[n-1]
代码语言:javascript
复制import numpy as np
a=np.array([1, 6, 7, 8, 12])
diff_x1 = np.diff(a)
print("diff_x1",diff_x1)
# diff_x1 [5 1 1 4]
# [6-1,7-6,8-7,12-8]
高维数组同样适用
二维数组
代码语言:javascript
复制b=np.array([[1, 6, 7, 8, 12],[1, 6, 7, 8, 12]])
diff_x2 = np.diff(b)
print("diff_x2 n",diff_x2)
# diff_x2
# [[5 1 1 4]
# [5 1 1 4]]
高维数组
代码语言:javascript
复制c=b.reshape(5,1,2)
diff_x3 = np.diff(c)
print("diff_x3 n",diff_x3)
# diff_x3
# [[[ 5]] [6-1]
#
# [[ 1]] [8-7]
#
# [[-11]] [1-12]
#
# [[ 1]] [7-6]
#
# [[ 4]]] [12-8]