【问题标题】:Specifying vocabulary input in LDA在 LDA 中指定词汇输入
【发布时间】:2020-06-01 21:51:07
【问题描述】:

我正在尝试了解如何在我的情况下使用 LDA。我有一个包含许多文档的语料库,我想看看一组非常具体的单词和 ngrams 是如何分布在主题中的。有没有办法将特定单词列表指定为主题建模的词汇表?

我一直在使用 gensim 实现,我相信参数 id2word 可以解决这个问题,但我不清楚文档。我的理解正确吗?

【问题讨论】:

  • 您是否只想在主题建模中使用特定的关键字列表???
  • 是的,这就是我想要的。

标签: python nlp cluster-analysis gensim lda


【解决方案1】:

LDA 的主题建模方法是将每个文档视为一定比例的主题集合。并且每个主题都作为关键字的集合,再次按一定比例。

一旦你给算法提供了主题的数量,它就会重新排列文档内的主题分布和主题内的关键字分布,以获得主题-关键字分布的良好组合。

LDA 主题模型的两个主要输入是字典或词汇表 (id2word) 和语料库。

您可以使用类似的方法来实现:

import gensim.corpora as corpora

# Create Dictionary/Vocabulary
id2word = corpora.Dictionary(data_lemmatized)

# Create Corpus
texts = data_lemmatized

# Term Document Frequency
corpus = [id2word.doc2bow(text) for text in texts]

【讨论】:

    【解决方案2】:

    您可以为此使用 Scikit learn Countvectorizer

    from sklearn.feature_extraction.text import CountVectorizer
    from gensim import matutils
    from gensim.models.ldamodel import LdaModel
    
    text = ['computer time graph', 'survey response eps', 'human system computer','machinelearning is very hot topic','python win the race for simplicity as compared to other programming language']
    
    # suppose this are the word that you want to be used in your vocab 
    vocabulary = ['machine','python','learning','human', 'system','hot','time']
    
    vect = CountVectorizer(vocabulary = vocabulary)
    x = vect.fit_transform(text)
    
    feature_name = vect.get_feature_names()
    
    # now you can use matutils helper function of gensim
    model = LdaModel(matutils.Sparse2Corpus(x),num_topic=3,id2word=dict([(i, s) for i, s in enumerate(feature_name)]))
    
    #printing the topic 
    model.show_topics()
    

     #to see the vocab that use being used 
     print(vect.get_feature_names())
      ['machine', 'python', 'learning', 'human', 'system', 'hot', 'time'] # you will get the feature that you want include
    

    【讨论】:

      猜你喜欢
      • 2017-04-09
      • 2014-11-28
      • 1970-01-01
      • 1970-01-01
      • 2021-03-10
      • 2017-08-08
      • 1970-01-01
      • 1970-01-01
      • 2019-08-16
      相关资源
      最近更新 更多