Why does numpy Dot product of 2d array with 1d array produce 1d array?

前端 未结 4 1042
感情败类
感情败类 2020-12-22 01:07

I try to run the code like below:

>>> import numpy as np
>>> A = np.array([[1,2], [3,4], [5,6]])
>>> A.shape
(3, 2)
>>> B         


        
4条回答
  •  暖寄归人
    2020-12-22 01:30

    A.shape is (3, 2), B.shape is (2,) this situation could directly use the rule #4 for the dot operation np.dot(A,B):

    If a is an N-D array and b is a 1-D array, it is a sum product over the last axis of a and b.

    Because the alignment will happen between B's 2 (only axis of B) and A's 2 (last axis of A) and 2 indeed equals 2, numpy will judge that this is absolutely legitimate for dot operation. Therefore these two "2" are "consumed", leaving A's (3,) "in the wild". This (3,) will therefore be the shape of the result.

提交回复
热议问题