Warm tip: This article is reproduced from serverfault.com, please click

python-取矩阵的点积时显示ValueError

(python - Showing ValueError while taking dot product of matrix)

发布于 2020-11-28 05:23:36

我希望将两个numpy矩阵A和B_transformed相乘:

    A = 
     [[-1.910095  ]
      [-1.20056174]
      [-0.77669163]
      [ 0.62406999]
      [ 1.1471159 ]
      [ 2.11616247]]

    B = 
      [[ 0.70710678 -0.70710678]
       [ 0.70710678  0.70710678]]

     B_transformed = B[1,:]
                   = [0.70710678 0.70710678]

我试过了:

product = np.dot(A,B_transformed)

但是我得到ValueError:

ValueError: shapes (6,1) and (2,) not aligned:

根据矩阵规则,允许(6,1)X(1,2)。那我为什么会得到valueError?

期望的输出

product = [[-1.35064113 -1.35064113]
           [-0.84892534 -0.84892534]
           [-0.54920392 -0.54920392]
           [ 0.44128412  0.44128412]
           [ 0.81113343  0.81113343]
           [ 1.49635283  1.49635283]]
Questioner
Omkar Salokhe
Viewed
11
meTchaikovsky 2020-11-28 13:33:45

由于形状B_transformedIS(2,)的基础上,numpy 广播numpy无法找到一个方法来广播B_transformed,以便进行matmulA

为了获得所需的输出,你需要

np.matmul(A,B_transformed[None,:])

B_transformed[None,:]将重塑B_transformed(1,2)