2012-11-11 51 views
3

我做SVD,當我嘗試運行我的代碼,我得到以下錯誤播出在一起:Python的ValueError異常:操作數無法與形狀

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 

回答

7

你正在做的分量之積。無論是做這些事情的矩陣或使用:

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

+0

你可能應該描述如何將一個'ndarray'轉換成'matrix',並且解釋說對於'matrix'對象,乘法運作如預期的那樣... –

相關問題