Numpy 转置乘法问题

问题描述 投票:0回答:3

我试图找到矩阵与其转置相乘的特征值,但我无法使用 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

相反,当将

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

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

有人可以告诉我我做错了什么吗?

python numpy scipy eigenvalue
3个回答
27
投票

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

另外,尝试将

testmatrix
dot()
函数相乘,即
numpy.dot(testmatrix,testmatrix.T)

显然

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


8
投票

您正在使用逐元素乘法 - 两个 Numpy 矩阵上的

*
运算符相当于 Matlab 中的
.*
运算符。使用

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

1
投票

表示此操作的另一种便捷方式是

testmatrix @ testmatrix.T

numpy中的

@
运算符表示矩阵乘法,可以在here

看到
© www.soinside.com 2019 - 2024. All rights reserved.