理解axis参数和dot函数

Ra1ny House bio photo By Ra1ny House

理解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维度向量内积,多维是第一个向量最后一维和第二个向量倒数第二维的乘积和

```python
>>> 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