简体   繁体   English

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

[英]Python ValueError: operands could not be broadcast together with shapes

I am doing SVD and when I try to run my code I get the following error:我正在做 SVD,当我尝试运行我的代码时,出现以下错误:

ValueError: operands could not be broadcast together with shapes (375, 375) (375, 500) ValueError: 操作数无法与形状一起广播 (375, 375) (375, 500)

I am using an image with size (500, 375)我正在使用大小为 (500, 375) 的图像

Here is my code:这是我的代码:

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

You are doing component wise product.您正在做组件明智的产品。 Either make those things matrices or use:要么制作这些东西矩阵,要么使用:

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

or use something like:或使用类似的东西:

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

Which is much cleaner if you do a lot of matrix products (like in this line), otherwise arrays are typically preferable since they are the base type.如果你做很多矩阵乘积(就像在这一行),这会更干净,否则数组通常更可取,因为它们是基本类型。 If you prefer you can of course also just call np.asmatrix on each itself.如果您愿意,您当然也可以在每个本身上调用np.asmatrix

暂无
暂无

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

相关问题 Python - ValueError:操作数无法与形状一起广播 - Python - ValueError: operands could not be broadcast together with shapes Python ValueError:操作数不能与形状(5,4)(5,5)一起广播 - Python ValueError: operands could not be broadcast together with shapes (5,4) (5,5) python numpy ValueError:操作数无法与形状一起广播 - python numpy ValueError: operands could not be broadcast together with shapes ValueError: 操作数无法与形状 (100,) (99,) 一起广播 Python - ValueError: operands could not be broadcast together with shapes (100,) (99,) error in Python Python:ValueError:操作数不能与形状一起广播(101)(2) - Python: ValueError: operands could not be broadcast together with shapes (101) (2) Pandas:ValueError - 操作数无法与形状一起广播 - Pandas: ValueError - operands could not be broadcast together with shapes ValueError: 操作数无法与形状 (7,) (6,) (7,) 一起广播 - ValueError: operands could not be broadcast together with shapes (7,) (6,) (7,) ValueError: 操作数无法与形状一起广播 (7410,) (3,) - ValueError: operands could not be broadcast together with shapes (7410,) (3,) ValueError:操作数不能与形状(5,)(30,)一起广播 - ValueError: operands could not be broadcast together with shapes (5,) (30,) QuantileRegression ValueError:操作数无法与形状一起广播 - QuantileRegression ValueError: operands could not be broadcast together with shapes
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM