【发布时间】:2016-12-04 13:39:03
【问题描述】:
我已经让gensim Word2Vec 实现为我计算了一些词嵌入。据我所知,一切都非常奇妙。现在我正在对创建的词向量进行聚类,希望得到一些语义分组。
下一步,我想查看每个集群中包含的单词(而不是向量)。 IE。如果我有嵌入向量[x, y, z],我想找出这个向量代表的实际单词。我可以通过调用model.vocab 和通过model.syn0 获取单词向量。但我找不到明确匹配这些的位置。
这比我预期的要复杂,我觉得我可能错过了明显的方法。任何帮助表示赞赏!
问题:
将单词与Word2Vec () 创建的嵌入向量匹配——我该怎么做?
我的做法:
创建模型后(代码如下*),我现在想将分配给每个单词的索引(在build_vocab() 阶段)与输出为model.syn0 的向量矩阵相匹配。
因此
for i in range (0, newmod.syn0.shape[0]): #iterate over all words in model
print i
word= [k for k in newmod.vocab if newmod.vocab[k].__dict__['index']==i] #get the word out of the internal dicationary by its index
wordvector= newmod.syn0[i] #get the vector with the corresponding index
print wordvector == newmod[word] #testing: compare result of looking up the word in the model -- this prints True
-
有没有更好的方法来做到这一点,例如通过将向量输入到模型中来匹配单词?
-
这甚至能让我得到正确的结果吗?
*我创建词向量的代码:
model = Word2Vec(size=1000, min_count=5, workers=4, sg=1)
model.build_vocab(sentencefeeder(folderlist)) #sentencefeeder puts out sentences as lists of strings
model.save("newmodel")
我发现this question 很相似,但还没有真正得到回答。
【问题讨论】:
标签: python vector machine-learning gensim word2vec