gpt4 book ai didi

python-3.x - 使用 Gensim 实现 Word 到矢量模型

转载 作者:行者123 更新时间:2023-11-30 08:34:38 26 4
gpt4 key购买 nike

我们正在尝试为下面给出的单词集实现一个单词向量模型。

stemmed = ['data', 'appli', 'scientist', 'mgr', 'microsoft', 'hire', 'develop', 'mentor', 'team', 'data', 'scientist', 'defin', 'data', 'scienc', 'prioriti', 'deep', 'understand', 'busi', 'goal', 'collabor', 'across', 'multipl', 'group', 'set', 'team', 'shortterm', 'longterm', 'goal', 'act', 'strateg', 'advisor', 'leadership', 'influenc', 'futur', 'direct', 'strategi', 'defin', 'partnership', 'align', 'efficaci', 'broad', 'analyt', 'effort', 'analyticsdata', 'team', 'drive', 'particip', 'data', 'scienc', 'bi', 'commun', 'disciplin', 'microsoftprior', 'experi', 'hire', 'manag', 'run', 'team', 'data', 'scientist', 'busi', 'domain', 'experi', 'use', 'analyt', 'must', 'experi', 'across', 'sever', 'relev', 'busi', 'domain', 'util', 'critic', 'think', 'skill', 'conceptu', 'complex', 'busi', 'problem', 'solut', 'use', 'advanc', 'analyt', 'larg', 'scale', 'realworld', 'busi', 'data', 'set', 'candid', 'must', 'abl', 'independ', 'execut', 'analyt', 'project', 'help', 'intern', 'client', 'understand']

我们正在使用此代码:

import gensim
model = gensim.models.FastText(stemmed, size=100, window=5, min_count=1, workers=4, sg=1)
model.wv.most_similar(positive=['data'])

但是,我们收到此错误:

KeyError: 'all ngrams for word data absent from model'

最佳答案

您需要提供训练数据,而不是以列表的形式,而是以生成器的形式

尝试:

import gensim
from gensim.models.fasttext import FastText as FT_gensim

stemmed = ['data', 'appli', 'scientist', ... ]

def gen_words(stemmed):
yield stemmed

model = FT_gensim(size=100, window=5, min_count=1, workers=4, sg=1)
model.build_vocab(gen_words(stemmed))

model.train(gen_words(stemmed), total_examples=model.corpus_count, epochs=model.iter)
model.wv.most_similar(positive=['data'])

打印出:

[('busi', -0.043828580528497696)]

另请参阅this notebook来自 gensim 文档。和this excellent gensim tutorial在所有可迭代的事物上:

In gensim, it’s up to you how you create the corpus. Gensim algorithms only care that you supply them with an iterable of sparse vectors (and for some algorithms, even a generator = a single pass over the vectors is enough).

关于python-3.x - 使用 Gensim 实现 Word 到矢量模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51988701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com