【问题标题】:Gensim: Unable to train the LDA modelGensim:无法训练 LDA 模型
【发布时间】:2017-03-25 17:15:18
【问题描述】:

我有一个句子列表,我按照tutorial 的说明从中制作语料库:

texts = [[word for word in document.lower().split() if word.isalpha()] for document in documents]
corpus = corpora.Dictionary(texts)

我想在这个语料库上训练一个 LDA 模型并提取主题关键字。

lda = models.LdaModel(corpus, num_topics=10)

但是,我在训练时收到错误消息:TypeError: 'int' object is not iterable。我究竟做错了什么?语料库的格式应该是什么?

【问题讨论】:

    标签: nlp gensim lda corpus


    【解决方案1】:

    制作语料库后,您应该使用doc2bow 制作单个语料库,它会从单词中生成哈希(所谓的'hashing trick'):

    texts = [[word for word in document.lower().split() if word.isalpha()] for document in documents]
    corpus = corpora.Dictionary(texts)
    hashed_corpus = [corpora.doc2bow(text) for text in texts]
    

    然后你可以用hashed_corpus训练你的模型:

    lda = models.LdaModel(corpus, id2word=corpus, num_topics=5) 
    

    id2word 将您的主题从哈希映射到单词,使用它可以将主题作为单词而不是数字来获取。

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 2018-09-08
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-01
      • 2018-07-31
      • 2016-06-07
      相关资源
      最近更新 更多