1

我已经在 text_corpus 上使用 gensim 训练了 LDA 模型。

>lda_model = gensim.models.ldamodel.LdaModel(text_corpus, 10)

现在,如果必须推断一个新的文本文档 text_sparse_vector 我必须做

>lda_model[text_sparse_vector]
[(0, 0.036479568280206563), (3, 0.053828073308160099), (7, 0.021936618544365804), (11, 0.017499953446152686), (15, 0.010153090454090822), (16, 0.35967516223499041), (19, 0.098570351997275749), (26, 0.068550060242800928), (27, 0.08371562828754453), (28, 0.14110945630261607), (29, 0.089938130046832571)]

但是我如何获得每个相应主题的单词分布。例如,我如何知道主题编号 16 的前 20 个单词?

gensim.models.ldamodel.LdaModel 类具有名为 show_topics(topics=10, topn=10, log=False, formatted=True) 的方法,但正如文档所述,它显示随机选择的主题列表。

有没有办法链接或打印我可以将推断的主题编号映射到单词分布?

4

3 回答 3

6
lda.print_topic(x, topn=20) 

将为您提供主题 x 的前 20 个功能

于 2013-01-30T22:38:00.603 回答
0

这里的最后一行将更改每个主题的字数。希望这可以帮助 :)

# train and save LDA model

lda_model = gensim.models.LdaMulticore(bow_corpus, num_topics=20, id2word=dictionary, passes=2, workers=2, chunksize=400000)

# check out the topics 

for idx, topic in lda_model.print_topics(-1):
   print('Topic: {} \nWords: {}'.format(idx, topic))

# swap out '30' for any number and this line will give you that many words per topic :)
lda_model.print_topics(idx, 30)

于 2019-04-01T16:35:17.997 回答
0

或者,如果您有K主题,那么:

print(str(["Topic #"+str(k)+":\n" + str(lda.show_topic(k,topn=20)) for k in range(K)]))

会让你变得丑陋,但始终排序的输出。

于 2017-01-27T00:49:47.987 回答