Numpy 转置乘法问题

发布于 2024-09-09 03:51:35 字数 590 浏览 0 评论 0原文

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

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说不完的你爱 2024-09-16 03:51:35

由于您了解 MATLAB,您可能会发现此教程很有用。

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

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

You might find this tutorial useful since you know MATLAB.

Also, try multiplying testmatrix with the dot() 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).

り繁华旳梦境 2024-09-16 03:51:35

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

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

You're using element-wise multiplication - the * operator on two Numpy matrices is equivalent to the .* operator in Matlab. Use

prod = numpy.dot(testmatrix, testmatrix.T)
风吹短裙飘 2024-09-16 03:51:35

表示此操作的另一种便捷方式是 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文