简体   繁体   中英

Topic Modelling: WordCloud For Every Topic in LDA model

Question: How can I create a Word Cloud for each topic that has been computed by the LDA model. I tried the following, but can't seem to work it out further to create a word cloud for each topic.

first_topic = lda.components_[0]
second_topic = lda.components_[1]
third_topic = lda.components_[2]
fourth_topic = lda.components_[3]

firstcloud = WordCloud(
                      background_color='black',
                      width=2500,
                      height=1800
                     ).generate(" ".join(first_topic))
plt.imshow(firstcloud)
plt.axis('off')
plt.show()

you can try this:

def create_wordcloud(model, topic):
    text = {word: value for word, value in model.show_topic(topic)}
    wc = WordCloud(background_color="white", max_words=1000)
    wc.generate_from_frequencies(text)
    plt.imshow(wc, interpolation="bilinear")
    plt.axis("off")
    plt.title("Topic" + " "+ str(topic))
    plt.show()

and then call this function as follow:

for i in range(1,num_topics):
    create_wordcloud(lda_model, topic=i)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM