【问题标题】:How can I print document wise topics in Gensim?如何在 Gensim 中打印文档主题?
【发布时间】:2019-08-12 07:35:16
【问题描述】:

我正在使用带有 gensim 的 LDA 进行主题建模。我的数据有 23 个文档,我希望每个文档都有单独的主题/单词,但 gensim 正在为整个文档集提供主题。如何为单个文档获取它?

dictionary = corpora.Dictionary(doc_clean)

# Converting list of documents (corpus) into Document Term Matrix using 
#dictionary prepared above.

corpus = [dictionary.doc2bow(doc) for doc in doc_clean]


# Creating the object for LDA model using gensim library
Lda = gensim.models.ldamodel.LdaModel

# Running and Trainign LDA model on the document term matrix.
ldamodel = Lda(corpus, num_topics=3, id2word = dictionary, passes=50)

result=ldamodel.print_topics(num_topics=3, num_words=3)

这是我得到的输出:

[(0, '0.011*"plex" + 0.010*"game" + 0.009*"racing"'),
(1, '0.008*"app" + 0.008*"live" + 0.007*"share"'),
(2, '0.015*"device" + 0.009*"file" + 0.008*"movie"')]

【问题讨论】:

    标签: python nltk gensim lda topic-modeling


    【解决方案1】:

    print_topics() 返回主题列表、加载到该主题的单词以及这些单词。

    如果您希望为每个文档加载主题,则需要使用get_document_topics()

    来自gensim documentation


    get_document_topics(bow, minimum_probability=None, minimum_phi_value=None, per_word_topics=False)

    获取给定文档的主题分布。

    参数: bow (corpus : list of (int, float)) – BOW 格式的文档。 minimum_probability (float) – 分配概率低于此阈值的主题将被丢弃。 minimum_phi_value (float) – 如果 per_word_topicsTrue,这表示包含的术语概率的下限。 如果设置为None,则使用1e-8 的值来防止0。 per_word_topics (bool) – 如果是 True,此函数还将返回两个额外列表,如“返回”部分所述。

    返回:
    list of (int, float) – 整个文档的主题分布。列表中的每个元素都是一对主题的 id 和分配给它的概率。

    list of (int, list of (int, float),可选 - 每个单词最可能的主题。列表中的每个元素都是一对单词的 id,以及按与该单词的相关性排序的主题列表。仅当 per_word_topics 设置为 True 时才返回。

    list of (int, list of float),可选 - Phi 相关值乘以特征长度,每个词-主题组合。列表中的每个元素是一对单词的 id 和该单词与每个主题之间的 phi 值列表。仅当 per_word_topics 设置为 True 时才返回。


    get_term_topics()get_topic_terms() 也可能对您感兴趣。

    【讨论】:

    • get_document_topics() 仅返回概率分布列表,我想要的是每个文档的单词或主题的名称。
    • 请澄清 - 主题建模中的主题没有名称,所以我仍然不确定你想要什么。您可以将“期望的输出”添加到您的问题中吗?
    • 在我发布的输出中,它显示了一些单词/名称以及它们的概率分布,但它适用于所有文档的组合,我希望每个文档都有这个东西,可能吗?跨度>
    • 我仍然认为您可能想要get_document_topics() - 它返回所有主题的列表以及每个文档加载到该主题的程度。这些主题没有“名称”——这些你必须自己想出——但有编号。因此,文档 1 可能对主题 1 的加载为 0.4,对主题 2 的加载为 0.3,等等...
    【解决方案2】:

    如果我理解正确,您需要将整个内容放入一个循环中并执行print_topics()

    您的文档示例:

    doc1 = "Brocolli is good to eat. My brother likes to eat good brocolli, but not my mother."
    doc2 = "My mother spends a lot of time driving my brother around to baseball practice."
    doc3 = "Some health experts suggest that driving may cause increased tension and blood pressure."
    doc_set = [doc_a, doc_b, doc_c]
    

    现在你的循环必须遍历你的doc_set:

    for i in doc_set:
          ##### after all the cleaning in these steps, append to a list #####
    
          dictionary = corpora.Dictionary(doc_clean)
          corpus = [dictionary.doc2bow(doc) for doc in doc_clean]
    
          ##### set the num_topics you want for each document, I set one for now #####
    
          ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics = 1, id2word = dictionary, passes=20)
          for i in ldamodel.print_topics():
              print(i)
              print('\n')
    

    样本输出:

    (0, '0.200*"brocolli" + 0.200*"eat" + 0.200*"good" + 0.133*"brother" + 0.133*"like" + 0.133*"mother"')
    
    
    (0, '0.097*"brocolli" + 0.097*"eat" + 0.097*"good" + 0.097*"mother" + 0.097*"brother" + 0.065*"lot" + 0.065*"spend" + 0.065*"practic" + 0.065*"around" + 0.065*"basebal"')
    
    
    (0, '0.060*"drive" + 0.060*"eat" + 0.060*"good" + 0.060*"mother" + 0.060*"brocolli" + 0.060*"brother" + 0.040*"pressur" + 0.040*"health" + 0.040*"caus" + 0.040*"increas"')
    

    【讨论】:

    • 我试过你的方法,但它给出的输出和以前一样,使用循环没有任何区别
    猜你喜欢
    • 1970-01-01
    • 2013-02-22
    • 2023-04-06
    • 2020-12-25
    • 2014-05-26
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2018-03-16
    相关资源
    最近更新 更多