每个主题添加单词 LDA

Add words per topic LDA

我正在使用 Gensim 在 python 中构建 LDA,我正在努力增加每个主题打印的字数,而不是默认的 10 个。我想要 20 个主题,每个主题 30 个字。任何建议将不胜感激:)

# train the LDA model

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

# check out the topics

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

您有两个选择:show_topicsprint_topics

来自gensim ldamulticore documentation

show_topicsprint_topics 的可自定义别名稍微多一点,可以在您的情况下提供更漂亮的输出)具有参数 num_words - 您要显示的字数,排名按重要性。

for idx, topic in lda_model.show_topics(idx, num_words=30):
   print('Topic: {} \nWords: {}'.format(idx, topic))

您也可以省略 idx - 输出不受其影响。 print_topics 工作方式类似,但默认显示 10 个主题:

for idx, topic in lda_model2.show_topics(num_topics=20, num_words=30):
   print('Topic: {} \nWords: {}'.format(idx, topic))