Numpy 转置乘法问题
我试图找到矩阵的特征值与其转置相乘,但我无法使用 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 应用程序中使用它。
有人可以告诉我我做错了什么吗?
I tried to find the eigenvalues of a matrix multiplied by its transpose but I couldn't do it using numpy.
testmatrix = numpy.array([[1,2],[3,4],[5,6],[7,8]])
prod = testmatrix * testmatrix.T
print eig(prod)
I expected to get the following result for the product:
5 11 17 23
11 25 39 53
17 39 61 83
23 53 83 113
and eigenvalues:
0.0000
0.0000
0.3929
203.6071
Instead I got ValueError: shape mismatch: objects cannot be broadcast to a single shape
when multiplying testmatrix
with its transpose.
This works (the multiplication, not the code) in MatLab but I need to use it in a python application.
Can someone tell me what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
由于您了解 MATLAB,您可能会发现此教程很有用。
另外,尝试将
testmatrix
与dot()
函数相乘,即numpy.dot(testmatrix,testmatrix.T)
显然
numpy.dot
用于数组之间进行矩阵乘法!*
运算符用于逐元素乘法(MATLAB 中的.*
)。You might find this tutorial useful since you know MATLAB.
Also, try multiplying
testmatrix
with thedot()
function, i.e.numpy.dot(testmatrix,testmatrix.T)
Apparently
numpy.dot
is used between arrays for matrix multiplication! The*
operator is for element-wise multiplication (.*
in MATLAB).您正在使用逐元素乘法 - 两个 Numpy 矩阵上的
*
运算符相当于 Matlab 中的.*
运算符。使用You're using element-wise multiplication - the
*
operator on two Numpy matrices is equivalent to the.*
operator in Matlab. Use表示此操作的另一种便捷方式是 testmatrix @ testmatrix.T。
numpy中的
@
运算符表示矩阵乘法,可以看出此处Another convenient way of representing this operation is
testmatrix @ testmatrix.T
.The
@
operator in numpy represents matrix multiplication, and can be seen here