python生成的词云没有图案,如何在Python中从LDA模型生成词云?

该博客介绍了如何使用Python的Gensim库进行LDA主题建模,并从每个主题中提取前20个关键词来创建词云。通过调用Gensim的`show_topic`方法,可以获取每个主题的关键词,并将其写入文件以供后续生成词云使用。
摘要由CSDN通过智能技术生成

I am doing some topic modeling on newspaper articles, and have implemented LDA using gensim in Python3. Now I want to create a word cloud for each topic, using the top 20 words for each topic. I know I can print the words, and save the LDA model, but is there any way to just save the top words for each topic which I can further use for generating word clouds?

I tried to google it, but could not find anything relevant. Any help is appreciated.

解决方案

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

好的,这是一个比较复杂的问题,需要分几个步骤来回答。 1. 生成词云 首先,我们需要安装 `wordcloud` 和 `jieba` 这两个库。`wordcloud` 用于生成词云,`jieba` 用于文分词。你可以使用以下命令来安装它们: ``` pip install wordcloud jieba ``` 接着,我们需要准备文本数据,并进行分词。假设我们有一个名为 `text.txt` 的文本文件,我们可以使用以下代码进行分词: ```python import jieba with open("text.txt", "r", encoding="utf-8") as f: text = f.read() words = jieba.cut(text) text = " ".join(words) ``` 接着,我们可以使用 `wordcloud` 生成词云。以下是一个简单的例子: ```python from wordcloud import WordCloud wc = WordCloud(width=800, height=400, background_color="white", max_words=100, font_path="simsun.ttf") wc.generate(text) wc.to_file("wordcloud.png") ``` 在这个例子,我们指定了词云的宽度、高度、背景色、最大词汇量和字体文件路径。然后,我们使用 `generate` 方法生成词云,并使用 `to_file` 方法保存词云图片。 2. 分析词云内容 生成词云之后,我们可以使用一些工具来分析词云内容。以下是一些常用的工具: - 词频统计。可以使用 Python 的 `collections` 模块的 `Counter` 类来统计词频。 ```python from collections import Counter counter = Counter(words) print(counter.most_common(10)) ``` 这个例子,我们统计了 `words` 出现次数最多的 10 个词汇。 - 情感分析。可以使用一些第三方库(如 `SnowNLP`)来对文本进行情感分析。 ```python from snownlp import SnowNLP s = SnowNLP(text) print(s.sentiments) ``` 这个例子,我们使用 `SnowNLP` 对文本进行情感分析,并输出情感值(越接近 1 表示越正面,越接近 0 表示越负面)。 - 主题分析。可以使用一些第三方库(如 `gensim`)来对文本进行主题分析。 ```python import gensim from gensim import corpora dictionary = corpora.Dictionary([words]) corpus = [dictionary.doc2bow(words)] lda_model = gensim.models.LdaModel(corpus, num_topics=5, id2word=dictionary) print(lda_model.print_topics()) ``` 这个例子,我们使用 `gensim` 对文本进行主题分析,并输出主题模型的主题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值