![](/img/trans.png)
[英]Python - ValueError: operands could not be broadcast together with shapes
[英]Python ValueError: operands could not be broadcast together with shapes
我正在做 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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.