dumbchild
dumbchild

Reputation: 285

calculate with values from two different dicts and return keys

I'm trying to calculate the cosine similarity between all values of dict1 and all values of dict2. when i'm done, i want to return the keys of the dicts where the similarity is high. To do that, I want to save the results of cosine similarity in a similarity dict. This is my attempt:

similarity_dictionary = {}
for x in dict1:
    for y in dict2:
        for x_key, x_val in dict1.items():
            for y_key, y_val in dict2.items():
                cos_sim =  numpy.dot(x_val, y_val)/(norm(x_val,)*norm(y_val))
                dict_of_sims[[x_key, y_key]] = cos_sim 

this gives me the following error:

ValueError: shapes (1,300) and (1,300) not aligned: 300 (dim 1) != 1 (dim 0)

Could someone please help with 1. explain the error and 2. lead me in the right direction? Thank you in advance!

Upvotes: 0

Views: 28

Answers (1)

user13561458
user13561458

Reputation:

It looks like you're trying to calculate a dot product of two 1x300 matrices. The error simply states that this cannot work, since you can only multiply an m x n matrix with an n x p matrix (i.e. the 'inner dimensions' need to be the same).

Also, it is hard to say how to improve your code if you don't provide a minimal working example.

Upvotes: 1

Related Questions