python 将两个不同维度的矩阵相乘

发布于 2025-01-18 21:23:06 字数 1098 浏览 0 评论 0原文

错误:valueerror:形状(3,1)和(3,2)未对齐:1(dim 1)!= 3(dim 0)

发生错误,因为矩阵的大小不同,但是如何我可以乘以两个具有不同大小的矩阵以及所得的输出应为:[-0.78 0.85]

import numpy as np

x1 = 3-7/3;
x2 = 2-4/3;
x3 = 1-5/3;

X = ([x1], [x2],[x3])

V = ([-0.99, -0.13], [-0.09, 0.70],[0.09, -0.70])

res = np.dot(X,V)
print("Res: ",res)

任何帮助都将受到赞赏!


数学问题,以更好地理解:

在由三个数据点x1,x2和x3组成的数据集中进行了主成分分析矩阵的每一行都是数据点。假设矩阵x ̃对应于 x ,每列的平均值substrics sublesed Ie

x =([[3.00,2.00,1.00],[4.00,1.00,2.00],[[4.00,1.00,2.00],[ 0.00,1.00,2.00])

和假设x ̃具有奇异的值分解:

v =([-0.99,-0.13,-0.00],[-0.09,0.70,-0.71],[0.09 ,-0.70,-0.71]))

第一个观察X1的X1的坐标是什么(舍入到两个有效的)坐标,该X1投影到包含最大变化的2维子空间上?

答案:

可以通过从 x 中提取平均值来找到投影 并投影到 v 的前两列。平均减法的第一个点具有坐标:[2-7/3 2-4/3 1-5/3]

(左)应乘以 v 的前两列:

(( [3-7/3],[2-4/3],[1-5/3]) *([-0.99,-0.13],[-0.09,0.70],[0.09,-0.70])= [ -0.78 0.85]


因此,我正在尝试找出如何在Python中计算出来。

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

The error occurs because the matrices are different sizes, but how can I multiply two matrices with different size and where the resulting output should be: [-0.78 0.85]?

import numpy as np

x1 = 3-7/3;
x2 = 2-4/3;
x3 = 1-5/3;

X = ([x1], [x2],[x3])

V = ([-0.99, -0.13], [-0.09, 0.70],[0.09, -0.70])

res = np.dot(X,V)
print("Res: ",res)

Any help is appreciated!


Mathematical question, for better understanding:

A principal component analysis is carried out on a dataset comprised of three data points x1, x2 and x3 collected in a N × M matrix X such that each row of the matrix is a data point. Suppose the matrix X ̃ corresponds to X with the mean of each columns substracted i.e.

X = ([3.00, 2.00, 1.00],[4.00, 1.00, 2.00],[0.00, 1.00, 2.00])

and suppose X ̃ has the singular value decomposition:

V = ([-0.99, -0.13, -0.00], [-0.09, 0.70, -0.71],[0.09, -0.70, -0.71])

What is the (rounded to two significant digits) coordinates of the first observation x1 projected onto the 2-Dimensional subspace containing the maximal variation?

Answer:

The projection can be found by substracting the mean from X
and projecting onto the first two columns of V. The first point with the mean subtracted has coordinates: [2-7/3 2-4/3 1-5/3]

This should be (left) multiplied with the first two columns of V:

([3-7/3], [2-4/3],[1-5/3]) * ([-0.99, -0.13], [-0.09, 0.70],[0.09, -0.70]) = [-0.78 0.85]


So I am trying to find out how to calculate this in python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

木有鱼丸 2025-01-25 21:23:06

我假设您希望执行矩阵乘法。如果矩阵的维数不同,则无法实现这一点。您可以使用 reshapenumpy.matmul() 获得所需的结果。
代码:

import numpy as np

x1 = 3-7/3;
x2 = 2-4/3;
x3 = 1-5/3;

X = np.array([[x1], [x2],[x3]])
X = X.reshape(1, 3)

V = np.array([[-0.99, -0.13], [-0.09, 0.70],[0.09, -0.70]])

res = np.matmul(X, V)
print("Res: ",res)

I am assuming you wish to perform matrix multliplication. This cannot be achieved if the dimensions of the matrices are different. You can achieve the desired result by using reshape and numpy.matmul().
Code:

import numpy as np

x1 = 3-7/3;
x2 = 2-4/3;
x3 = 1-5/3;

X = np.array([[x1], [x2],[x3]])
X = X.reshape(1, 3)

V = np.array([[-0.99, -0.13], [-0.09, 0.70],[0.09, -0.70]])

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