我正在执行SVD,当我尝试运行代码时,出现以下错误:
ValueError:操作数不能与形状(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
。
使用此链接进行参考:blog它会为您提供规则等。