【问题标题】:How to print out the full distribution of words in an LDA topic in gensim?如何在gensim中打印出LDA主题中单词的完整分布?
【发布时间】:2013-07-13 19:54:39
【问题描述】:

下面代码中的lda.show_topics模块只打印每个主题前10个词的分布,我如何打印出语料库中所有词的完整分布?

from gensim import corpora, models

documents = ["Human machine interface for lab abc computer applications",
"A survey of user opinion of computer system response time",
"The EPS user interface management system",
"System and human system engineering testing of EPS",
"Relation of user perceived response time to error measurement",
"The generation of random binary unordered trees",
"The intersection graph of paths in trees",
"Graph minors IV Widths of trees and well quasi ordering",
"Graph minors A survey"]

stoplist = set('for a of the and to in'.split())
texts = [[word for word in document.lower().split() if word not in stoplist]
         for document in documents]

dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]

lda = models.ldamodel.LdaModel(corpus_tfidf, id2word=dictionary, num_topics=2)

for i in lda.show_topics():
    print i

【问题讨论】:

  • 您可以做些骇人听闻的事情,并更改站点包(或您计算机上的任何位置)中的 lda 包以打印所有这些,或将其代码复制到您的程序中,然后将其更改为打印全部而不是 10。
  • 刚刚找到答案,它有点隐藏在 api =) 中。请参阅下面的答案
  • 很好地找到了自己的答案。

标签: python lda topic-modeling gensim


【解决方案1】:

show_topics() 中有一个变量调用 topn,您可以在其中指定每个主题的单词分布中所需的前 N ​​个单词的数量。见http://radimrehurek.com/gensim/models/ldamodel.html

所以不是默认的lda.show_topics()。您可以使用len(dictionary) 获取每个主题的完整单词分布:

for i in lda.show_topics(topn=len(dictionary)):
    print i

【讨论】:

    【解决方案2】:

    show_topics()中有两个变量调用num_topicsnum_words,对于num_topics的主题数量,返回num_words最重要的词(每个主题10个词,默认情况下)。见http://radimrehurek.com/gensim/models/ldamodel.html#gensim.models.ldamodel.LdaModel.show_topics

    因此,您可以使用 len(lda.id2word) 表示每个主题的完整单词分布,并使用 lda.num_topics 表示 lda 模型中的所有主题。

    for i in lda.show_topics(formatted=False,num_topics=lda.num_topics,num_words=len(lda.id2word)):
        print i
    

    【讨论】:

    • 请解释你的答案。 SO 的存在不仅仅是为了回答问题,而是为了帮助人们学习。仅代码答案被视为低质量
    【解决方案3】:

    以下代码将打印您的单词及其概率。我打印了前 10 个单词。您可以更改 num_words = 10 以打印每个主题的更多单词。

    for words in lda.show_topics(formatted=False,num_words=10):
        print(words[0])
        print("******************************")
        for word_prob in words[1]:
            print("(",dictionary[int(word_prob[0])],",",word_prob[1],")",end = "")
        print("")
        print("******************************")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-31
      • 2015-06-27
      • 2013-06-23
      • 2023-04-06
      • 2017-08-08
      • 2017-02-19
      • 2020-12-25
      • 2018-09-06
      相关资源
      最近更新 更多