理解axis參數和dot函數

理解axis看下面的例子

多維數組的軸(axis=)是和該數組的size(或者shape)的元素是相對應的;

>>> np.random.seed(123)
>>> X = np.random.randint(0, 5, [3, 2, 2])
>>> print(X)

[[[5 2]
  [4 2]]

 [[1 3]
  [2 3]]

 [[1 1]
  [0 1]]]

>>> X.sum(axis=0)
array([[7, 6],
       [6, 6]])

>>> X.sum(axis=1)
array([[9, 4],
       [3, 6],
       [1, 2]])

>>> X.sum(axis=2)
array([[7, 6],
       [4, 5],
       [2, 1]])

對于dot,官方解釋如下:

For 2-D arrays it is equivalent to matrix multiplication, and for 1-D arrays to inner product of vectors (without complex conjugation). For N dimensions it is a sum product over the last axis of a and the second-to-last of b:

2維是矩陣乘法,1維度向量內積,多維是第一個向量最后一維和第二個向量倒數第二維的乘積和

>>> a = np.arange(3*4*5*6).reshape((3,4,5,6))
>>> b = np.arange(3*4*5*6)[::-1].reshape((5,4,6,3))
>>> np.dot(a, b)[2,3,2,1,2,2]
499128
>>> sum(a[2,3,2,:] * b[1,2,:,2])
499128
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容