【问题标题】:Numpy transpose multiplication problemNumpy转置乘法问题
【发布时间】:2011-03-13 21:20:43
【问题描述】:

我试图找到矩阵的特征值乘以其转置,但我无法使用 numpy。

testmatrix = numpy.array([[1,2],[3,4],[5,6],[7,8]])
prod = testmatrix * testmatrix.T
print eig(prod)

我预计该产品会得到以下结果:

5    11    17    23
11    25    39    53
17    39    61    83
23    53    83   113

和特征值:

0.0000
0.0000
0.3929
203.6071

当将testmatrix 与其转置相乘时,我得到了ValueError: shape mismatch: objects cannot be broadcast to a single shape

这在 MatLab 中有效(乘法,而不是代码),但我需要在 python 应用程序中使用它。

谁能告诉我我做错了什么?

【问题讨论】:

    标签: python numpy scipy eigenvalue


    【解决方案1】:

    您可能会发现这个tutorial 很有用,因为您了解 MATLAB。

    另外,尝试将testmatrixdot() 函数相乘,即numpy.dot(testmatrix,testmatrix.T)

    显然numpy.dot 在数组之间用于矩阵乘法! * 运算符用于逐元素乘法(MATLAB 中的 .*)。

    【讨论】:

    • PEP 465 允许使用中缀 @ 运算符:mat1 @ mat2
    【解决方案2】:

    您正在使用逐元素乘法 - 两个 Numpy 矩阵上的 * 运算符等效于 Matlab 中的 .* 运算符。使用

    prod = numpy.dot(testmatrix, testmatrix.T)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-09
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多