【问题标题】:How do I print lda topic model and the word cloud of each of the topics如何打印 lda 主题模型和每个主题的词云
【发布时间】:2017-03-09 17:58:45
【问题描述】:
from nltk.tokenize import RegexpTokenizer
from stop_words import get_stop_words
from gensim import corpora, models
import gensim
import os
from os import path
from time import sleep
import matplotlib.pyplot as plt
import random
from wordcloud import WordCloud, STOPWORDS
tokenizer = RegexpTokenizer(r'\w+')
en_stop = set(get_stop_words('en'))
with open(os.path.join('c:\users\kaila\jobdescription.txt')) as f:
    Reader = f.read()

Reader = Reader.replace("will", " ")
Reader = Reader.replace("please", " ")


texts = unicode(Reader, errors='replace')
tdm = []

raw = texts.lower()
tokens = tokenizer.tokenize(raw)
stopped_tokens = [i for i in tokens if not i in en_stop]
tdm.append(stopped_tokens)

dictionary = corpora.Dictionary(tdm)
corpus = [dictionary.doc2bow(i) for i in tdm]
sleep(3)
ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics=8, id2word = dictionary)
topics = ldamodel.print_topics(num_topics=8, num_words=200)
for i in topics:
    print(i)
    wordcloud = WordCloud().generate(i)
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.show()

问题在于词云。我无法为 8 个主题中的每一个获得词云。我想要一个为 8 个主题提供 8 个词云的输出。 如果有人可以帮助我解决这个问题,那就太好了。

【问题讨论】:

    标签: python topic-modeling word-cloud


    【解决方案1】:

    假设您已经训练了一个 gensim lda 模型,您可以使用以下代码简单地创建一个词云

    # lda is assumed to be the variable holding the LdaModel object
    import matplotlib.pyplot as plt
    for t in range(lda.num_topics):
        plt.figure()
        plt.imshow(WordCloud().fit_words(lda.show_topic(t, 200)))
        plt.axis("off")
        plt.title("Topic #" + str(t))
        plt.show()
    

    我将突出显示您的代码中的一些错误,以便您更好地遵循我上面写的内容。

    WordCloud().generate(something) 期望 something 是原始文本。它将对其进行标记、小写并删除停用词,然后计算词云。您需要单词大小来匹配它们在主题中的概率(我假设)。

    lda.print_topics(8, 200) 返回主题的文本表示,如prob1*"token1" + prob2*"token2" + ... 您需要lda.show_topic(topic, num_words) 以获取具有相应概率的单词作为元组。然后你需要WordCloud().fit_words()来生成词云。

    以下代码是您使用上述可视化的代码。我还想指出,您从单个文档中推断出非常罕见的主题,并且可能不是您想要的。

    from nltk.tokenize import RegexpTokenizer
    from stop_words import get_stop_words
    from gensim import corpora, models
    import gensim
    import os
    from os import path
    from time import sleep
    import matplotlib.pyplot as plt
    import random
    from wordcloud import WordCloud, STOPWORDS
    tokenizer = RegexpTokenizer(r'\w+')
    en_stop = set(get_stop_words('en'))
    with open(os.path.join('c:\users\kaila\jobdescription.txt')) as f:
        Reader = f.read()
    
    Reader = Reader.replace("will", " ")
    Reader = Reader.replace("please", " ")
    
    
    texts = unicode(Reader, errors='replace')
    tdm = []
    
    raw = texts.lower()
    tokens = tokenizer.tokenize(raw)
    stopped_tokens = [i for i in tokens if not i in en_stop]
    tdm.append(stopped_tokens)
    
    dictionary = corpora.Dictionary(tdm)
    corpus = [dictionary.doc2bow(i) for i in tdm]
    ldamodel = gensim.models.ldamodel.LdaModel(corpus, num_topics=8, id2word = dictionary)
    for t in range(ldamodel.num_topics):
        plt.figure()
        plt.imshow(WordCloud().fit_words(ldamodel.show_topic(t, 200)))
        plt.axis("off")
        plt.title("Topic #" + str(t))
        plt.show()
    

    尽管来自不同的库,您可以看到 topic visualizations with corresponding code 的结果(免责声明:我是该库的作者之一)。

    【讨论】:

    • 非常感谢。这当然解决了我的问题。很抱歉,我现在无法投票,因为我还没有获得这样做的声誉
    • 我实际上已经抓取了 jobsdb 数据并用于分析。抓取的数据被编译到一个用于主题建模的文件中。
    • 感谢您的回答。然而,在最新版本的 wordcloud 中,fit_words 接受一个字典,而lda.show_topic 返回一个元组列表。我必须使用plt.imshow(WordCloud().fit_words(dict(lda.show_topic(t, 200)))) 行才能让它工作。
    【解决方案2】:

    以下内容对我有用: 首先,创建一个 lda 模型并定义集群/主题,如 Topic Clustering 中所述 - 确保 minimum_probability 为 0。 接下来,使用lda_corpus = lda[corpus] 确定 LDA 语料库 现在将属于每个主题的数据中的文档识别为列表,下面的示例有两个主题。 df 是我的原始数据,其中包含一列 texts

    cluster1 = [j for i,j in zip(lda_corpus,df.texts) if i[0][1] > .2]
    cluster2 = [j for i,j in zip(lda_corpus,df.texts) if i[1][1] > .2]
    

    获取每个集群的词云。您可以包含尽可能多的停用词。确保清理集群中的数据,例如删除停用词、词干提取等。我将跳过这些步骤,以便每个集群都有清理的文本/文档。

    wordcloud = WordCloud(relative_scaling = 1.0, stopwords=("xxx", 'yyy').generate(' '. join(cluster1))
    

    最后使用 matplotlib 绘制词云

    plt.imshow(wordcloud)
    

    【讨论】:

      猜你喜欢
      • 2023-04-06
      • 1970-01-01
      • 2012-03-25
      • 2013-06-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多