使用gensim了解LDA实现

时间:2013-12-03 11:31:05

标签: python topic-modeling gensim dirichlet

我试图了解Python中的gensim包如何实现Latent Dirichlet Allocation。我正在做以下事情:

定义数据集

documents = ["Apple is releasing a new product", 
             "Amazon sells many things",
             "Microsoft announces Nokia acquisition"]             

删除停用词后,我创建了词典和语料库:

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

然后我定义了LDA模型。

lda = gensim.models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, update_every=1, chunksize=10000, passes=1)

然后我打印主题:

>>> lda.print_topics(5)
['0.181*things + 0.181*amazon + 0.181*many + 0.181*sells + 0.031*nokia + 0.031*microsoft + 0.031*apple + 0.031*announces + 0.031*acquisition + 0.031*product', '0.077*nokia + 0.077*announces + 0.077*acquisition + 0.077*apple + 0.077*many + 0.077*amazon + 0.077*sells + 0.077*microsoft + 0.077*things + 0.077*new', '0.181*microsoft + 0.181*announces + 0.181*acquisition + 0.181*nokia + 0.031*many + 0.031*sells + 0.031*amazon + 0.031*apple + 0.031*new + 0.031*is', '0.077*acquisition + 0.077*announces + 0.077*sells + 0.077*amazon + 0.077*many + 0.077*nokia + 0.077*microsoft + 0.077*releasing + 0.077*apple + 0.077*new', '0.158*releasing + 0.158*is + 0.158*product + 0.158*new + 0.157*apple + 0.027*sells + 0.027*nokia + 0.027*announces + 0.027*acquisition + 0.027*microsoft']
2013-12-03 13:26:21,878 : INFO : topic #0: 0.181*things + 0.181*amazon + 0.181*many + 0.181*sells + 0.031*nokia + 0.031*microsoft + 0.031*apple + 0.031*announces + 0.031*acquisition + 0.031*product
2013-12-03 13:26:21,880 : INFO : topic #1: 0.077*nokia + 0.077*announces + 0.077*acquisition + 0.077*apple + 0.077*many + 0.077*amazon + 0.077*sells + 0.077*microsoft + 0.077*things + 0.077*new
2013-12-03 13:26:21,880 : INFO : topic #2: 0.181*microsoft + 0.181*announces + 0.181*acquisition + 0.181*nokia + 0.031*many + 0.031*sells + 0.031*amazon + 0.031*apple + 0.031*new + 0.031*is
2013-12-03 13:26:21,881 : INFO : topic #3: 0.077*acquisition + 0.077*announces + 0.077*sells + 0.077*amazon + 0.077*many + 0.077*nokia + 0.077*microsoft + 0.077*releasing + 0.077*apple + 0.077*new
2013-12-03 13:26:21,881 : INFO : topic #4: 0.158*releasing + 0.158*is + 0.158*product + 0.158*new + 0.157*apple + 0.027*sells + 0.027*nokia + 0.027*announces + 0.027*acquisition + 0.027*microsoft
>>> 

我无法理解这个结果。它是否提供了每个单词出现的概率?另外,主题#1,主题#2等的含义是什么?我期待的东西或多或少像最重要的关键词。

我已经检查了gensim tutorial,但实际上并没有多大帮助。

感谢。

5 个答案:

答案 0 :(得分:18)

您正在寻找的答案位于gensim tutoriallda.printTopics(k)打印k个随机选择主题的贡献最多的单词。可以假设这是(部分地)在每个给定主题上的单词分布,意味着这些单词出现在左侧主题中的概率。

通常,人们会在大型语料库上运行LDA。在一个非常小的样本上运行LDA将无法获得最佳结果。

答案 1 :(得分:16)

我认为本教程将帮助您非常清楚地理解所有内容 - https://www.youtube.com/watch?v=DDq3OVp9dNA

我最初也面临很多理解它的问题。我将简要概述几点。

在潜在的Dirichlet分配中,

  • 单词的顺序在文档中并不重要 - Bag of Words模型。
  • 文档是对主题
  • 的分发
  • 每个主题依次是属于词汇 的分布
  • LDA是概率生成模型。它用于使用后验分布推断隐藏变量。

想象一下创建一个像这样的文档的过程 -

  1. 选择主题分发
  2. 绘制主题 - 并从主题中选择单词。对每个主题重复此操作
  3. LDA在这条线上有点回溯 - 你有一袋代表文件的文字,它代表的主题是什么?

    所以,在你的情况下,第一个主题(0)

    INFO : topic #0: 0.181*things + 0.181*amazon + 0.181*many + 0.181*sells + 0.031*nokia + 0.031*microsoft + 0.031*apple + 0.031*announces + 0.031*acquisition + 0.031*product
    

    更多地关注thingsamazonmany,因为它们的比例较高,而microsoftapple的比例则更低值。

    我建议您阅读此博客以获得更好的理解(Edwin Chen是天才!) - http://blog.echen.me/2011/08/22/introduction-to-latent-dirichlet-allocation/

答案 2 :(得分:9)

由于上述答案已经公布,现在有一些非常好的可视化工具可以使用function user_is_in(){ $is_logged_in = $this->session->userdata('is_logged_in'); if($is_logged_in != ""){ $this->members(); }else { redirect('verify/signin'); die(); //session has to be altered bro } } function members() { redirect('verify/users_mahali'); } 来获得LDA的直觉。

看一下pyLDAvis包。这是一个很棒的notebook overview。这是一个非常有用的video description面向最终用户(9分钟教程)。

希望这有帮助!

答案 3 :(得分:2)

为了理解gensim LDA实现的用法,我最近写了几篇博客文章,从头开始在Python中对70,000个简单wiki转储文章进行主题建模。

在这里,详细解释了gensim的LDA如何用于主题建模。 人们可以找到

的用法
ElementTree library for extraction of article text from XML dumped file.
Regex filters to clean the articles.
NLTK stop words removal & Lemmatization
LDA from gensim library

希望它有助于理解gensim包的LDA实现。

第1部分

Topic Modelling (Part 1): Creating Article Corpus from Simple Wikipedia dump

第2部分

Topic Modelling (Part 2): Discovering Topics from Articles with Latent Dirichlet Allocation

我得到的几个主题的词云(10个单词)作为结果。 enter image description here

答案 4 :(得分:0)

它返回该词与该主题相关联的百分比可能性。默认情况下,LDA向您显示前十个字词:)