gpt4 book ai didi

python - 值错误 : operands could not be broadcast together with shapes (224, 224) (180,180)

转载 作者:行者123 更新时间:2023-11-28 22:49:21 24 4
gpt4 key购买 nike

我正在编写一个程序来计算两个向量之间的余弦相似度。对于小文本文件,它工作正常,但对于大数据,它会出错。我已经经历了很多广播的例子,但无法解决实际问题。 (在 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)

最佳答案

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

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

例如

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,)

但是

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

会工作

关于python - 值错误 : operands could not be broadcast together with shapes (224, 224) (180,180),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23906324/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com