简体   繁体   English

点积ValueError:形状未对齐

[英]dot product ValueError: shapes not aligned

how to fix this如何解决这个问题

V = np.array([[-0.42866713, -0.56630692, -0.7039467 ],
              [ 0.80596391,  0.11238241, -0.58119908],
              [ 0.40824829, -0.81649658,  0.40824829]])
S = np.array([9.508032  , 0.77286964])
np.dot(S, V) #throws error ValueError: shapes (2,) and (3,3) not aligned: 2 (dim 0) != 3 (dim 0)

While this works:虽然这有效:

U = np.array([[-0.3863177 , -0.92236578],
              [-0.92236578,  0.3863177 ]])
S = np.array([9.508032  , 0.77286964])
np.dot(S, U)

I am trying to do SVD reconstruction:我正在尝试进行 SVD 重建:

M = np.array([[1,2,3], [4,5,6]])
U,S,V = np.linalg.svd(M)

# then i wanted do like np.product(np.product(U, S), V)
# or they do like np.product(U*S, V)

https://numpy.org/doc/stable/reference/generated/numpy.linalg.svd.html https://numpy.org/doc/stable/reference/generated/numpy.linalg.svd.html

As the ValueError suggests, S should have 3 values or V should have only 2 columns.正如ValueError所暗示的,S 应该有 3 个值,或者 V 应该只有 2 列。

I fixed it by calling:我通过调用修复它:

U * S @ V[:2]

V[:2] means keep only first 2 rows and all columns from matrix V V[:2] 表示只保留矩阵 V 的前 2 行和所有列

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM