0

I have two dictionaries:

dict1 = {'A':'3','B':'6','E':'9'}   

dict2 = {'A':'4','B':'8','C':'12','D':'16','E':'20'}

I need to extract key-value pairs from dict2 such that those keys are present in dict1 as well and then find the cosine similarity.

I need to make a new dictionary:

dict3 = {'A':'4','B':'8','E':'20'} 

from dict2, how do I do that? I have tried looping through both dictionaries but I'm not able to append.

How do I find the cosine similarity between dict1 & dict3? Should the values be converted to vectors first or is there a way to find it by keeping them as dictionaries?

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • By discarding the elements which are not in `dict1` you are artificially making the two more similar. Do you really want to regard `{'A':'3', 'B':'6', 'E':'9'}` as identical to `{'A':'3', 'B':'6', 'C':'12', 'D': '16', 'E':'9'}`? The usual solution to this would be to add the missing elements with a weight of 0; then, the individual similarity between e.g. the sublists `{'D': '16'}` and `{'D': '0'}` is not 100%. – tripleee Jun 09 '22 at 13:03

2 Answers2

0

You can simply use dictionary comprehension

def cosine_similarity(dict1, dict2):
    return { _k:_v for _k, _v in dict2.items() if _k in dict1} 


dict3 = cosine_similarity(dict1, dict2)

will give you that

{'A': '4', 'B': '8', 'E': '20'}
0

First part of your question you can achieve doing a dict-comprehension. Second part of your question is based on this question.

from scipy import spatial

dict1 = {"A": "3", "B": "6", "E": "9"}

dict2 = {"A": "4", "B": "8", "C": "12", "D": "16", "E": "20"}

dict3 = {key: int(value) for key, value in dict2.items() if key in dict1}

dict1_values = list(map(int, dict1.values()))
# [3,6,9]
dict3_values = list(dict3.values())
# [4,8,20]
cos_sim = 1 - spatial.distance.cosine(dict1_values, dict3_values)
print(cos_sim)

0.9759000729485332
Rabinzel
  • 7,757
  • 3
  • 10
  • 30