Python 培训gensim word2vec模型后单词不在词汇表中,为什么?

Python 培训gensim word2vec模型后单词不在词汇表中,为什么?,python,gensim,word2vec,Python,Gensim,Word2vec,所以我想使用单词嵌入,以获得一些方便的dandy余弦相似度值。在创建模型并检查数据中单词not的相似性后,我给出了模型,它告诉我单词不在词汇表中 为什么它找不到“不”这个词的相似之处呢 描述数据如下所示: [[‘不’、‘只’、‘做’、‘角’、‘做’、‘接合’、‘加强’、‘它们’、‘也’、‘提供’、‘更多’、‘一致’、‘直’、‘角’、‘辛普森’、‘强铁’、‘提供’、‘a’、‘宽’、‘品种’、‘角’、‘in’、‘各种’、‘尺寸’、‘和’、‘厚度’、‘to’、‘手柄’、‘轻型’、‘工作’、‘或’、‘项

所以我想使用单词嵌入,以获得一些方便的dandy余弦相似度值。在创建模型并检查数据中单词not的相似性后,我给出了模型,它告诉我单词不在词汇表中

为什么它找不到“不”这个词的相似之处呢

描述数据如下所示: [[‘不’、‘只’、‘做’、‘角’、‘做’、‘接合’、‘加强’、‘它们’、‘也’、‘提供’、‘更多’、‘一致’、‘直’、‘角’、‘辛普森’、‘强铁’、‘提供’、‘a’、‘宽’、‘品种’、‘角’、‘in’、‘各种’、‘尺寸’、‘和’、‘厚度’、‘to’、‘手柄’、‘轻型’、‘工作’、‘或’、‘项目’、‘何n、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、,‘型号’、‘编号’、‘接头’、‘用于’、‘各种’、‘连接’、‘和’、‘家用’、‘修理’、‘项目强力’、‘比’、‘角度’、‘钉’、‘或’、‘螺钉’、‘紧固’、‘单独帮助’、‘确保’、‘接头’、‘一致’、‘直’、‘和’、‘strong尺寸’、‘in’、‘x’、‘in’、‘inmade’、‘from’、‘量规’、‘钢镀锌’、‘for’、‘extra’、‘腐蚀’、‘抗安装’、‘带’、‘d’、‘普通’、‘钉子’、‘或’、‘x’、‘in’、‘strongdrive’、‘sd’、‘螺钉’]]

请注意,我已经尝试将数据作为单独的句子而不是单独的单词给出

def word_vec_sim_sum(row):
    description = row.product_description.split()
    description_embedding = gensim.models.Word2Vec([description], size=150,
        window=10,
        min_count=2,
        workers=10,
        iter=10)       
    print(description_embedding.wv.most_similar(positive="not"))
你需要降低最小值

从:min_count int,可选–忽略总频率低于此值的所有单词。在您提供的数据中,不会出现一次,因此将被忽略。通过将min_count设置为1,可以工作

import gensim as gensim

data = [['not', 'only', 'do', 'angles', 'make', 'joints', 'stronger', 'they', 'also', 'provide', 'more', 'consistent',
         'straight', 'corners', 'simpson', 'strongtie', 'offers', 'a', 'wide', 'variety', 'of', 'angles', 'in',
         'various', 'sizes', 'and', 'thicknesses', 'to', 'handle', 'lightduty', 'jobs', 'or', 'projects', 'where', 'a',
         'structural', 'connection', 'is', 'needed', 'some', 'can', 'be', 'bent', 'skewed', 'to', 'match', 'the',
         'project', 'for', 'outdoor', 'projects', 'or', 'those', 'where', 'moisture', 'is', 'present', 'use', 'our',
         'zmax', 'zinccoated', 'connectors', 'which', 'provide', 'extra', 'resistance', 'against', 'corrosion', 'look',
         'for', 'a', 'z', 'at', 'the', 'end', 'of', 'the', 'model', 'numberversatile', 'connector', 'for', 'various',
         'connections', 'and', 'home', 'repair', 'projectsstronger', 'than', 'angled', 'nailing', 'or', 'screw',
         'fastening', 'alonehelp', 'ensure', 'joints', 'are', 'consistently', 'straight', 'and', 'strongdimensions',
         'in', 'x', 'in', 'x', 'inmade', 'from', 'gauge', 'steelgalvanized', 'for', 'extra', 'corrosion',
         'resistanceinstall', 'with', 'd', 'common', 'nails', 'or', 'x', 'in', 'strongdrive', 'sd', 'screws']]


def word_vec_sim_sum(row):
    description = row
    description_embedding = gensim.models.Word2Vec([description], size=150,
                                                   window=10,
                                                   min_count=1,
                                                   workers=10,
                                                   iter=10)
    print(description_embedding.wv.most_similar(positive="not"))


word_vec_sim_sum(data[0])
以及输出:

[('do', 0.21456070244312286), ('our', 0.1713767945766449), ('can', 0.1561305820941925), ('repair', 0.14236785471439362), ('screw', 0.1322808712720871), ('offers', 0.13223429024219513), ('project', 0.11764446645975113), ('against', 0.08542445302009583), ('various', 0.08226475119590759), ('use', 0.08193354308605194)]
你需要降低最小值

从:min_count int,可选–忽略总频率低于此值的所有单词。在您提供的数据中,不会出现一次,因此将被忽略。通过将min_count设置为1,可以工作

import gensim as gensim

data = [['not', 'only', 'do', 'angles', 'make', 'joints', 'stronger', 'they', 'also', 'provide', 'more', 'consistent',
         'straight', 'corners', 'simpson', 'strongtie', 'offers', 'a', 'wide', 'variety', 'of', 'angles', 'in',
         'various', 'sizes', 'and', 'thicknesses', 'to', 'handle', 'lightduty', 'jobs', 'or', 'projects', 'where', 'a',
         'structural', 'connection', 'is', 'needed', 'some', 'can', 'be', 'bent', 'skewed', 'to', 'match', 'the',
         'project', 'for', 'outdoor', 'projects', 'or', 'those', 'where', 'moisture', 'is', 'present', 'use', 'our',
         'zmax', 'zinccoated', 'connectors', 'which', 'provide', 'extra', 'resistance', 'against', 'corrosion', 'look',
         'for', 'a', 'z', 'at', 'the', 'end', 'of', 'the', 'model', 'numberversatile', 'connector', 'for', 'various',
         'connections', 'and', 'home', 'repair', 'projectsstronger', 'than', 'angled', 'nailing', 'or', 'screw',
         'fastening', 'alonehelp', 'ensure', 'joints', 'are', 'consistently', 'straight', 'and', 'strongdimensions',
         'in', 'x', 'in', 'x', 'inmade', 'from', 'gauge', 'steelgalvanized', 'for', 'extra', 'corrosion',
         'resistanceinstall', 'with', 'd', 'common', 'nails', 'or', 'x', 'in', 'strongdrive', 'sd', 'screws']]


def word_vec_sim_sum(row):
    description = row
    description_embedding = gensim.models.Word2Vec([description], size=150,
                                                   window=10,
                                                   min_count=1,
                                                   workers=10,
                                                   iter=10)
    print(description_embedding.wv.most_similar(positive="not"))


word_vec_sim_sum(data[0])
以及输出:

[('do', 0.21456070244312286), ('our', 0.1713767945766449), ('can', 0.1561305820941925), ('repair', 0.14236785471439362), ('screw', 0.1322808712720871), ('offers', 0.13223429024219513), ('project', 0.11764446645975113), ('against', 0.08542445302009583), ('various', 0.08226475119590759), ('use', 0.08193354308605194)]

可能是因为你在将标记化的句子传递给你的模型之前删除了停止词。可能是因为它没有经过足够的训练来捕获单词的嵌入。如果既不是也不是,你必须处理缺少的单词,如何处理?将分数设为-1,因为如果单词不在那里,你就无法将它与另一个单词进行比较,因为你不知道ve表示其嵌入的向量。可能是因为您在将标记化的句子传递给模型之前删除了停止词。可能是因为它没有经过足够的时间来捕获单词的嵌入。如果既不包含也不包含,您必须处理缺少的单词,如何处理?将分数指定为-1,因为如果单词不存在,则您无法访问将其与另一个单词进行比较,因为没有表示其嵌入的向量。