计算 python 中两个词频词典的余弦相似度的正确方法?
Right way to calculate the cosine similarity of two word-frequency-dictionaries in python?
我正在尝试遍历包含文本的文件并计算当前行与用户提出的查询之间的余弦相似度。我已经标记了查询和行并将它们的词并集保存到一个集合中。
示例:
line_tokenized = ['Karl', 'Donald', 'Ifwerson']
query_tokenized = ['Donald', 'Trump']
word_set = ['Karl', 'Donald', 'Ifwerson', 'Trump']
现在我必须为行和查询分别创建一个字典,包含词频对。我想到了这样的事情:
line_dict = {'Karl': 1, 'Donald': 1, 'Ifwerson': 1, 'Trump': 0}
query_dict = {'Karl': 0, 'Donald': 1, 'Ifwerson': 0, 'Trump': 1}
但是由于键值对是无序的,因此无法正确计算余弦相似度。我遇到了 OrderedDict()
,但我不明白如何实现某些东西,因为它的元素存储为元组:
所以我的问题是:
- 如何设置键值对并在之后访问它们?
- 如何增加某个键的值?
- 或者还有其他更简单的方法吗?
Cosine similarity无需订购字典,简单查找即可:
import math
def cosine_dic(dic1,dic2):
numerator = 0
dena = 0
for key1,val1 in dic1.items():
numerator += val1*dic2.get(key1,0.0)
dena += val1*val1
denb = 0
for val2 in dic2.values():
denb += val2*val2
return numerator/math.sqrt(dena*denb)
您只需使用 .get(key1,0.0)
来查找元素是否存在,如果不存在则假定为 0.0
。因此,dic1
和 dic2
都不需要存储以 0
作为值的值。
回答您的其他问题:
How can I set the key-value pairs and have access to them afterwards?
您只需说明:
dic[key] = value
How can I increment the value of a certain key?
如果您确定键已经是字典的一部分:
dic[key] += 1
否则你可以使用:
dic[key] = dic.get(key,0)+1
Or is there any other more easier way to do this?
您可以使用 Counter
,它基本上是具有一些附加功能的字典。
使用 pandas
和 scipy
import pandas as pd
from scipy.spatial.distance import cosine
line_dict = {'Karl': 1, 'Donald': 1, 'Ifwerson': 1, 'Trump': 0}
query_dict = {'Karl': 0, 'Donald': 1, 'Ifwerson': 0, 'Trump': 1}
line_s = pd.Series(line_dict)
query_s = pd.Series(query_dict)
print(1 - cosine(line_s, query_s))
这段代码会输出0.40824829046386291
我不明白你所说的 "order" 是什么意思,所以我没有处理这个问题,但这段代码对你来说应该是一个好的开始。
我正在尝试遍历包含文本的文件并计算当前行与用户提出的查询之间的余弦相似度。我已经标记了查询和行并将它们的词并集保存到一个集合中。
示例:
line_tokenized = ['Karl', 'Donald', 'Ifwerson']
query_tokenized = ['Donald', 'Trump']
word_set = ['Karl', 'Donald', 'Ifwerson', 'Trump']
现在我必须为行和查询分别创建一个字典,包含词频对。我想到了这样的事情:
line_dict = {'Karl': 1, 'Donald': 1, 'Ifwerson': 1, 'Trump': 0}
query_dict = {'Karl': 0, 'Donald': 1, 'Ifwerson': 0, 'Trump': 1}
但是由于键值对是无序的,因此无法正确计算余弦相似度。我遇到了 OrderedDict()
,但我不明白如何实现某些东西,因为它的元素存储为元组:
所以我的问题是:
- 如何设置键值对并在之后访问它们?
- 如何增加某个键的值?
- 或者还有其他更简单的方法吗?
Cosine similarity无需订购字典,简单查找即可:
import math
def cosine_dic(dic1,dic2):
numerator = 0
dena = 0
for key1,val1 in dic1.items():
numerator += val1*dic2.get(key1,0.0)
dena += val1*val1
denb = 0
for val2 in dic2.values():
denb += val2*val2
return numerator/math.sqrt(dena*denb)
您只需使用 .get(key1,0.0)
来查找元素是否存在,如果不存在则假定为 0.0
。因此,dic1
和 dic2
都不需要存储以 0
作为值的值。
回答您的其他问题:
How can I set the key-value pairs and have access to them afterwards?
您只需说明:
dic[key] = value
How can I increment the value of a certain key?
如果您确定键已经是字典的一部分:
dic[key] += 1
否则你可以使用:
dic[key] = dic.get(key,0)+1
Or is there any other more easier way to do this?
您可以使用 Counter
,它基本上是具有一些附加功能的字典。
使用 pandas
和 scipy
import pandas as pd
from scipy.spatial.distance import cosine
line_dict = {'Karl': 1, 'Donald': 1, 'Ifwerson': 1, 'Trump': 0}
query_dict = {'Karl': 0, 'Donald': 1, 'Ifwerson': 0, 'Trump': 1}
line_s = pd.Series(line_dict)
query_s = pd.Series(query_dict)
print(1 - cosine(line_s, query_s))
这段代码会输出0.40824829046386291
我不明白你所说的 "order" 是什么意思,所以我没有处理这个问题,但这段代码对你来说应该是一个好的开始。