How to generate word clouds from LDA models in Python?

北慕城南 提交于 2019-11-30 10:39:16
Kenneth Orton

You can get the topn words from an LDA model using Gensim's built-in method show_topic.

lda = models.LdaModel.load('lda.model')

for i in range(0, lda.num_topics):
    with open('output_file.txt', 'w') as outfile:
        outfile.write('{}\n'.format('Topic #' + str(i + 1) + ': '))
        for word, prob in lda.show_topic(i, topn=20):
            outfile.write('{}\n'.format(word.encode('utf-8')))
        outfile.write('\n')

This will write a file with a format similar to this:

Topic #69: 
pet
dental
tooth
adopt
animal
puppy
rescue
dentist
adoption
animal
shelter
pet
dentistry
vet
paw
pup
patient
mix
foster
owner

Topic #70: 
periscope
disneyland
disney
snapchat
brandon
britney
periscope
periscope
replay
britneyspear
buffaloexchange
britneyspear
https
meerkat
blab
periscope
kxci
toni
disneyland
location

You may or may not need to adjust this to your needs, ie yield a list of top 20 words instead of outputting it to a text file.

The answer in this post gives a good explanation of how to use raw text to create the word clouds. How do I print lda topic model and the word cloud of each of the topics

is there any way to just save the top words for each topic ?

Yes there is. jLDADMM outputs the top topical words for each topic. In version 1.0, only top topical words are written in the top-word output file, without their probabilities given the topic.

You may also consider using pyldavis package which can be used to visualize LDA models generated through gensim. An example is shown here

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!