np.diff函數


np.diff函數

覺得有用的話,歡迎一起討論相互學習~

我的微博我的github我的B站

數組中a[n]-a[n-1]

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]

高維數組同樣適用

二維數組

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]]

高維數組

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]


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM