我正在尝试对电子邮件文档(包含单词的字符串)进行分类练习。
我将距离函数定义如下:
def distance(wordset1, wordset2):
if len(wordset1) < len(wordset2):
return len(wordset2) - len(wordset1)
elif len(wordset1) > len(wordset2):
return len(wordset1) - len(wordset2)
elif len(wordset1) == len(wordset2):
return 0
但是,最终的准确率相当低(0.8)。我想这是因为距离函数不太准确。如何改进功能?或者还有哪些其他方法可以计算电子邮件文档之间的“距离”?
最佳答案
在这种情况下使用的一种常见的相似性度量是 Jaccard similarity 。它的范围从 0 到 1,其中 0 表示完全不相似,1 表示两个文档相同。它被定义为
wordSet1 = set(wordSet1)
wordSet2 = set(wordSet2)
sim = len(wordSet1.intersection(wordSet2))/len(wordSet1.union(wordSet2))
本质上,它是单词集合的交集与单词集合的并集的比率。这有助于控制不同大小的电子邮件,同时仍然提供良好的相似性度量。
关于python - 如何改进Python中的距离函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40074155/