【问题标题】:Gensim LDA topic assignmentGensim LDA 主题分配
【发布时间】:2017-02-19 13:29:55
【问题描述】:

我希望使用 LDA 将每个文档分配给一个主题。现在我意识到你得到的是来自 LDA 的主题分布。但是,正如您从下面的最后一行看到的那样,我将其分配给最可能的主题。

我的问题是这样的。为了获得这些主题,我必须第二次运行lda[corpus]。是否有其他一些内置的 gensim 函数可以直接给我这个主题分配向量?特别是由于 LDA 算法已经通过了文档,它可能已经保存了这些主题分配?

    # Get the Dictionary and BoW of the corpus after some stemming/ cleansing
    texts = [[stem(word) for word in document.split() if word not in STOPWORDS] for document in cleanDF.text.values]
    dictionary = corpora.Dictionary(texts)
    dictionary.filter_extremes(no_below=5, no_above=0.9)
    corpus = [dictionary.doc2bow(text) for text in texts]

    # The actual LDA component
    lda = models.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=30, chunksize=10000, passes=10,workers=4) 

    # Assign each document to most prevalent topic
    lda_topic_assignment = [max(p,key=lambda item: item[1]) for p in lda[corpus]]

【问题讨论】:

    标签: gensim lda topic-modeling


    【解决方案1】:

    没有其他内置的 Gensim 函数可以直接给出主题分配向量。

    您的问题是有效的,LDA 算法已通过文档,但 LDA 的实现通过更新块中的模型(基于 chunksize 参数的值)来工作,因此它不会将整个语料库保留在内存中。

    因此您必须使用lda[corpus] 或使用方法lda.get_document_topics()

    【讨论】:

      【解决方案2】:
      dictionary = corpora.Dictionary(texts)
      corpus = [dictionary.doc2bow(text) for text in texts]
      
      
      test =LDA[corpus[0]]
      print(test)
      sorted(test, reverse=True, key=lambda x: x[1])
      
      Topics = ['Topic_'+str(sorted(LDA[i], reverse=True, key=lambda x: x[1])[0][0]).zfill(3) for i in corpus]
      

      【讨论】:

      • 请提供一些上下文来解释为什么您的解决方案是正确的。这将有助于 OP 理解它。
      猜你喜欢
      • 2013-06-23
      • 2020-12-25
      • 2015-06-27
      • 2022-01-14
      • 1970-01-01
      • 1970-01-01
      • 2019-03-31
      • 1970-01-01
      • 2017-09-25
      相关资源
      最近更新 更多