【问题标题】:ValueError: shapes (1,1000) and (1,1000) not aligned: 1000 (dim 1) != 1 (dim 0) When numpy.dot() with two matricesValueError:形状(1,1000)和(1,1000)未对齐:1000(dim 1)!= 1(dim 0)当numpy.dot()有两个矩阵时
【发布时间】:2017-12-21 23:52:24
【问题描述】:

我正在尝试使用 numpy.dot() 将两个矩阵相乘,它们的维度相同。但是当我尝试这样做时,numpy 给了我标题中的错误,说它们没有对齐,这对我来说没有意义,因为它们是相同的尺寸。谁能向我解释我做错了什么?

【问题讨论】:

  • 请添加代码和示例矩阵
  • 两个 1x1000 矩阵之间的矩阵乘法没有意义。具有相同的维度并不是矩阵乘法有效的标准。

标签: python numpy matrix


【解决方案1】:

形状是问题

a = np.arange(3).reshape(1,3)
b = np.arange(3,6).reshape(1,3)

np.dot(a, b)

Traceback (most recent call last):

  File "<ipython-input-88-c9cc415545b4>", line 1, in <module>
    np.dot(a, b)

ValueError: shapes (1,3) and (1,3) not aligned: 3 (dim 1) != 1 (dim 0)


c = b.reshape(3,1)

np.dot(a,c)
array([[14]])

【讨论】:

    猜你喜欢
    • 2019-03-21
    • 2019-05-28
    • 2021-04-25
    • 2021-05-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-26
    • 2019-04-25
    相关资源
    最近更新 更多