Python ValueError:操作数无法与形状一起广播 (224,224) (180,180)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23906324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 03:38:46  来源:igfitidea点击:

ValueError: operands could not be broadcast together with shapes (224,224) (180,180)

pythonnumpy

提问by user3568044

I am writing a program to find the cosine similarity between two vectors. For small text files it works fine but for large datas it gives error. I have gone through many examples of broadcasting but couldn't get the actual problem. (Getting an error at line p=x*y)

我正在编写一个程序来查找两个向量之间的余弦相似度。对于小文本文件,它工作正常,但对于大数据,它会出错。我经历了许多广播的例子,但无法解决实际问题。(在 p=x*y 行出错)

x = numpy.dot(u, u.T)
y = numpy.dot(v, v.T)
p = x * y
value = numpy.dot(u, v.T) / p

p=(x*y)
ValueError: operands could not be broadcast together with shapes (224,224) (180,180)

回答by ThePredator

Your variable xand variable yhave different "dimensions". You should try to make sure they have similar "dimensions", i.e 224,224 and 224,224 or 180,180 and 180,180.

您的变量x和变量y具有不同的“维度”。您应该尝试确保它们具有相似的“维度”,即 224,224 和 224,224 或 180,180 和 180,180。

I.e with numpy "multiplication" it will not be possible to multiple two numpy arrayswith different "dimensions".

即使用 numpy“乘法”将不可能将两个具有不同“维度”的numpy 数组相乘。

For example

例如

x = np.linspace(1,10,num=224)
y = np.linspace(1,10,num=180)
p = x*y

Traceback (most recent call last):
  File "<pyshell#3>", line 1, in <module>
    p = x*y
ValueError: operands could not be broadcast together with shapes (224,) (180,) 

But

 x = np.linspace(1,10,num=224)
 y = np.linspace(1,10,num=224)
 p = x*y

will work

将工作

回答by MONTYHS

If xand ydo not have the same shape, then you will get this type of error. they all must be the same shape. Please read thisnumpybroadcast rule

如果xy不具有相同的形状,那么您将收到此类错误。它们都必须是相同的形状。请阅读numpy广播规则