Python Spacy初学者:相似功能
发布时间:2020-12-20 12:11:23 所属栏目:Python 来源:网络整理
导读:在 Python的spaCy教程示例中,apples.similarity(oranges)的结果是 0.39289959293092641 而不是0.7857989796519943 有什么理由吗? 本教程的原始文档 https://spacy.io/docs/ 一个教程与我得到的答案不同: http://textminingonline.com/getting-started-with
|
在
Python的spaCy教程示例中,apples.similarity(oranges)的结果是
0.39289959293092641 而不是0.7857989796519943 有什么理由吗? 谢谢 解决方法
这似乎是spacy中的一个错误.
不知何故,vector_norm计算错误. import spacy
import numpy as np
nlp = spacy.load("en")
# using u"apples" just as an example
apples = nlp.vocab[u"apples"]
print apples.vector_norm
# prints 1.4142135381698608,or sqrt(2)
print np.sqrt(np.dot(apples.vector,apples.vector))
# prints 1.0
然后,vector_norm用于相似性,它总是返回一个始终是正确值的一半的值. def similarity(self,other):
if self.vector_norm == 0 or other.vector_norm == 0:
return 0.0
return numpy.dot(self.vector,other.vector) / (self.vector_norm * other.vector_norm)
如果您对同义词的相似性分数进行排名,则可能没问题.但是如果你需要正确的余弦相似度得分,那么结果是不正确的. 我提交了问题here.希望很快就会得到修复. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
