Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用gensim理解LDA实现_Python_Gensim_Lda_Topic Modeling_Dirichlet - Fatal编程技术网

Python 使用gensim理解LDA实现

Python 使用gensim理解LDA实现,python,gensim,lda,topic-modeling,dirichlet,Python,Gensim,Lda,Topic Modeling,Dirichlet,我试图理解Python中的gensim包如何实现潜在的Dirichlet分配。我正在做以下工作: 定义数据集 documents = ["Apple is releasing a new product", "Amazon sells many things", "Microsoft announces Nokia acquisition"] 删除stopwords后,我创建了字典和语料库: texts = [[w

我试图理解Python中的gensim包如何实现潜在的Dirichlet分配。我正在做以下工作:

定义数据集

documents = ["Apple is releasing a new product", 
             "Amazon sells many things",
             "Microsoft announces Nokia acquisition"]             
删除stopwords后,我创建了字典和语料库:

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等的含义是什么?我期待着一些或多或少像最重要的关键词

我已经检查过了,但是没有多大帮助


谢谢。

您要找的答案在中
lda.printTopics(k)
k
随机选择的主题打印贡献最大的单词。我们可以假设这是(部分)单词在每个给定主题上的分布,这意味着这些单词出现在主题左侧的概率


通常,人们会在大型语料库上运行LDA。在一个小得可笑的样本上运行LDA不会得到最好的结果。

我认为本教程将帮助您非常清楚地理解所有内容-

一开始我在理解它的时候也遇到了很多问题。我将试着概括几点

在潜在Dirichlet分配中

  • 在单词文档袋模型中,单词的顺序并不重要
  • 文档是对主题的分发
  • 每个主题依次是属于词汇表的单词的分布
  • LDA是一种概率生成模型。它用于使用后验分布推断隐藏变量
想象一下创建文档的过程是这样的-

  • 在主题上选择一个发行版
  • 画一个主题,然后从主题中选择单词。对每个主题重复此步骤
  • LDA是沿着这条路线的一种回溯——假设您有一袋表示文档的单词,那么它代表的主题可能是什么

    因此,在您的案例中,第一个主题(0)

    更多的是关于
    事物
    亚马逊
    许多
    ,因为它们的比例更高,而不是关于
    微软
    苹果
    的,后者的价值要低得多


    我建议阅读本博客以便更好地理解(陈德文是个天才!)-

    既然上面的答案已经发布,现在有一些非常好的可视化工具,可以使用
    gensim
    获得LDA直觉

    看看派尔戴维斯的包裹。这是一个很好的例子。这里有一个面向最终用户的非常有用的教程(9分钟教程)


    希望这有帮助

    为了了解gensim LDA实现的用法,我最近写了一些博客文章,在70000篇简单的wiki转储的Python文章中从头开始实现主题建模

    在这里,有一个关于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实现

    第一部分

    第二部分

    作为结果,我得到了一些主题的词云(10个词)。

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

    。感谢您的回复……您知道如何将语料库分成五个不同的主题吗?此外,是否有可能单独挑选最热门的单词,而不是获得每个主题的单词分布。。。?我同意这是一个非常小的样本,但在尝试使用更大的样本之前,我想先了解它…因为3个文档确实是一个小数字,巨大的是相对的,我们设法在数百个文档的语料库中获得了显著的结果,当语料库基数超过数万时,你会很难理解。@user295338你可能需要阅读一些关于LDA及其应用程序的文章,这是一个好的开始。@user295338是的,但解释起来很复杂,尤其是在这种媒体上。如果你在数学上已经成熟,我建议你读一些论文,如果你还不成熟,那就需要大量阅读才能理解。幸运的是,LDA有很多资源。如果你有很强的统计和概率背景,你会很好,否则这将是一条漫长的道路,但这是值得的!至于获得某个主题中概率最高的单词,你可以对输出进行排序,然后从那里开始。@user295338如果你缺乏数学(概率和其他)背景,我刚刚在Quora找到了LDA的一个很好的解释,只是为了让你知道这些数字是该主题中每个单词的相对重要性。它们没有添加到1的原因是默认情况下
    print_topics
    show 10。如果显示100左右,总和将开始接近1。参见
    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