为什么下面的代码给了我 0.00 与两个具有相同单词宇航员的文档的相似性?
import spacy
nlp = spacy.en.English()
print (nlp('astronaut').similarity(nlp('astronaut')))
# Result: 0.0
原因是单个项的词向量是点,并且无法获得点之间的余弦距离相似性。
请参阅多维向量与点的向量比较:
>>> a = nlp(u'astronaut eating apple banana cherry')
>>> b = nlp(u'astronaut eating apple banana fruit')
>>> a.similarity(b)
0.96363932891327542
>>> a.similarity(a)
0.99999997666693974
>>> b.similarity(b)
1.000000996690289
>>> a = nlp(u'astronaut')
>>> b = nlp(u'astronaut')
>>> a.similarity(a)
0.0
>>> b = nlp(u'cosmonaut')
>>> a.similarity(a)
0.0
>>> b.similarity(b)
0.0
>>> a.similarity(b)
0.0
>>> c = nlp(u'single')
>>> a.similarity(c)
0.0