user3568044
user3568044

Reputation: 163

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

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)

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)

Upvotes: 2

Views: 24620

Answers (2)

MONTYHS
MONTYHS

Reputation: 924

If x and y do not have the same shape, then you will get this type of error. they all must be the same shape. Please read this numpy broadcast rule

Upvotes: 2

Srivatsan
Srivatsan

Reputation: 9363

Your variable x and variable y have 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.

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

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

Upvotes: 2

Related Questions