Python ValueError:操作数无法与形状一起广播

我正在做SVD,当我尝试运行我的代码时,我收到以下错误:

ValueError: operands could not be broadcast together with shapes (375, 375) (375, 500)

我正在使用尺寸为的图像(500,375)

这是我的代码:

from PIL import Image
from Image import new
from numpy import *
import numpy as np
from scipy.linalg import svd

im = Image.open("lake.tif")
pix = im.load()
im.show()
r, g, b = im.split()
R = np.array(r.getdata())
R.shape = (500, 375)
Ur, Sr, VrT = svd(R.T, full_matrices=False)
R1 = Ur * diag(Sr) * VrT

最佳答案 你正在做组件明智的产品.要么做那些矩阵或使用:

 R1 = np.dot(Ur, np.dot(diag(SR), VrT))

或使用类似的东西:

Ur, Sr, VrT = map(np.asmatrix, (Ur, diag(Sr), Vrt))
R1 = Ur * Sr * VrT

如果你做了很多矩阵产品(比如这一行),哪个更清洁,否则数组通常是优选的,因为它们是基类型.如果你愿意,你当然也可以在每个人身上调用np.asmatrix.

点赞