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 cosign 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?
CodePudding user response:
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'}
CodePudding user response:
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