python - 从 gensim 解释负 Word2Vec 相似度

标签 python nlp similarity gensim word2vec

例如我们使用 gensim 训练一个 word2vec 模型:

from gensim import corpora, models, similarities
from gensim.models.word2vec import Word2Vec

documents = ["Human machine interface for lab abc computer applications",
              "A survey of user opinion of computer system response time",
              "The EPS user interface management system",
              "System and human system engineering testing of EPS",
              "Relation of user perceived response time to error measurement",
              "The generation of random binary unordered trees",
              "The intersection graph of paths in trees",
              "Graph minors IV Widths of trees and well quasi ordering",
              "Graph minors A survey"]

texts = [[word for word in document.lower().split()] for document in documents]
w2v_model = Word2Vec(texts, size=500, window=5, min_count=1)

当我们查询单词之间的相似度时,我们发现负相似度分数:

>>> w2v_model.similarity('graph', 'computer')
0.046929569156789336
>>> w2v_model.similarity('graph', 'system')
0.063683518562347399
>>> w2v_model.similarity('survey', 'generation')
-0.040026775040430063
>>> w2v_model.similarity('graph', 'trees')
-0.0072684112978664561

我们如何解释负分?

如果是余弦相似度,范围不应该是[0,1]吗?

Word2Vec.similarity(x,y) 函数的上限和下限是多少? 文档里写的不多:https://radimrehurek.com/gensim/models/word2vec.html#gensim.models.word2vec.Word2Vec.similarity =(

查看 Python 包装器代码,没有太多:https://github.com/RaRe-Technologies/gensim/blob/develop/gensim/models/word2vec.py#L1165

(如果可能,请给我指出实现相似度函数的.pyx代码。)

最佳答案

余弦相似度范围为 -1 到 1,与正余弦波相同。

Cosine Wave

至于来源:

https://github.com/RaRe-Technologies/gensim/blob/ba1ce894a5192fc493a865c535202695bb3c0424/gensim/models/word2vec.py#L1511

def similarity(self, w1, w2):
    """
    Compute cosine similarity between two words.
    Example::
      >>> trained_model.similarity('woman', 'man')
      0.73723527
      >>> trained_model.similarity('woman', 'woman')
      1.0
    """
    return dot(matutils.unitvec(self[w1]), matutils.unitvec(self[w2])

关于python - 从 gensim 解释负 Word2Vec 相似度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42381902/

相关文章:

python - 如何使用 Moviepy 制作空白视频

machine-learning - NLP-句子切分

java - 计算两个字符串有多少个重复字符

算法 - 根据最相似的偏好对用户进行分组

python - 通过串行数据使用 Python mplotlib 动画化 3D 散点图

python - 如何从图像中提取签名(Python脚本)?

python - ElasticSearch 查询 pandas 数据框

nlp - Word2Vec 中的维度从何而来?

python - Spacy匹配器: TypeError: an integer is required

python - 文本相似度分析(Excel)