【问题标题】:Classify Text with Gensim LDA Model使用 Gensim LDA 模型对文本进行分类
【发布时间】:2021-01-02 18:28:23
【问题描述】:

作为参考,我已经看过以下问题:

  1. Gensim LDA for text classification
  2. Python Gensim LDA Model show_topics funciton

我希望通过 Gensim 训练我的 LDA 模型,根据模型创建的主题之一对句子进行分类。 排长队的东西

lda = models.LdaModel(corpus=corpus, id2word=id2word, num_topics=7, passes=20)
lda.print_topics()
for line in document: # where each line in the document is its own sentence for simplicity
    print('Sentence: ', line)
    topic = lda.parse(line) # where the classification would occur
    print('Topic: ', topic)

我知道 gensim 没有parse 功能,但是如何实现呢?这是我一直在关注的文档,但我没有得到它:

https://radimrehurek.com/gensim/auto_examples/core/run_topics_and_transformations.html#sphx-glr-auto-examples-core-run-topics-and-transformations-py

提前致谢。

编辑:更多文档-https://radimrehurek.com/gensim/models/ldamodel.html

【问题讨论】:

    标签: python python-3.x gensim lda


    【解决方案1】:

    让我来解决您的问题: 您想在某些文档上训练 LDA 模型并检索 7 个主题。然后,您想在其中一个(或多个?)主题中对新文档进行分类,这意味着您想推断新的、未见文档的主题分布。

    如果是这样,gensim 文档会提供答案。

    lda = models.LdaModel(corpus=corpus, id2word=id2word, num_topics=7, passes=20)
    lda.print_topics()
    count = 1
    for line in document: # where each line in the document is its own sentence for simplicity
        print('\nSentence: ', line)
        line = line.split()
        line_bow = id2word.doc2bow(line)
        doc_lda = lda[line_bow]
        print('\nLine ' + str(count) + ' assigned to Topic ' + str(max(doc_lda)[0]) + ' with ' + str(round(max(doc_lda)[1]*100,2)) + ' probability!')
        count += 1
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-31
      • 1970-01-01
      • 2018-10-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      相关资源
      最近更新 更多