Python实现LDA主题模型以及模型可视化

实现思路

  • 采用jieba进行数据处理
  • 采用gensim构建主题模型
  • 采用pyLDAvis可视化主题模型

包下载、引入

下载依赖包

pip install jieba
pip install gensim
pip install pyLDAvis

引入依赖包

import pyLDAvis.gensim_models
import jieba.posseg as jp,jieba
from gensim.models.coherencemodel import CoherenceModel
from gensim.models.ldamodel import LdaModel
from gensim.corpora.dictionary import Dictionary

jieba数据处理

打开两个文档,作为文档集

# 读取需要处理的文本
doc1=open('./data/text1.txt','rb').read()
doc2=open('./data/text2.txt','rb').read()

# 构建文本集
texts = [doc1,doc2]
# 词性标注条件
flags = ('n', 'nr', 'ns', 'nt', 'eng', 'v', 'd','vn','vd')
# 停用词表,打开一张停用词表
stopwords = open("./stop_words.txt","r",encoding='utf-8').read()
# 分词
words_ls = []
for text in texts:
    # 采用jieba进行分词、词性筛选、去停用词
    words = [word.word for word in jp.cut(text) if word.flag in flags and word.word not in stopwords]
    words_ls.append(words)

LDA模型构建

# 构造词典
dictionary = Dictionary(words_ls)
# 基于词典,使【词】→【稀疏向量】,并将向量放入列表,形成【稀疏向量集】
corpus = [dictionary.doc2bow(words) for words in words_ls]
# lda模型,num_topics设置主题的个数
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=3, random_state=100, iterations=50)
# U_Mass Coherence
ldaCM = CoherenceModel(model=lda, corpus=corpus, dictionary=dictionary, coherence='u_mass')

# 打印所有主题,每个主题显示10个词
for topic in lda.print_topics(num_words=10):
    print(topic)

LDA模型可视化

# 用pyLDAvis将LDA模式可视化
plot =pyLDAvis.gensim_models.prepare(lda,corpus,dictionary)
# 保存到本地html
pyLDAvis.save_html(plot, './result/pyLDAvis.html')

完整代码

# encoding=utf-8
import pyLDAvis.gensim_models
import jieba.posseg as jp,jieba
from gensim.models.coherencemodel import CoherenceModel
from gensim.models.ldamodel import LdaModel
from gensim.corpora.dictionary import Dictionary

# 读取需要处理的文本
doc1=open('./data/text1.txt','rb').read()
doc2=open('./data/text2.txt','rb').read()

# 构建文本集
texts = [doc1,doc2]
# 词性标注条件
flags = ('n', 'nr', 'ns', 'nt', 'eng', 'v', 'd','vn','vd')
# 停用词表
stopwords = open("./stop_words.txt","r",encoding='utf-8').read()
# 分词
words_ls = []
for text in texts:
    # 采用jieba进行分词
    words = [word.word for word in jp.cut(text) if word.flag in flags and word.word not in stopwords]
    words_ls.append(words)

# 构造词典
dictionary = Dictionary(words_ls)
# 基于词典,使【词】→【稀疏向量】,并将向量放入列表,形成【稀疏向量集】
corpus = [dictionary.doc2bow(words) for words in words_ls]
# lda模型,num_topics设置主题的个数
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=3, random_state=100, iterations=50)
# U_Mass Coherence
ldaCM = CoherenceModel(model=lda, corpus=corpus, dictionary=dictionary, coherence='u_mass')

# 打印所有主题,每个主题显示10个词
for topic in lda.print_topics(num_words=10):
    print(topic)

# 用pyLDAvis将LDA模式可视化
plot =pyLDAvis.gensim_models.prepare(lda,corpus,dictionary)
# 保存到本地html
pyLDAvis.save_html(plot, './result/pyLDAvis.html')

运行

python main.py

运行完成后,本地打开对应路径下的pyLDAvis.html,可以得到可视化结果
在这里插入图片描述

代码地址:详见

  • 21
    点赞
  • 297
    收藏
    觉得还不错? 一键收藏
  • 18
    评论
LDA(Latent Dirichlet Allocation)是一种主题模型,它可以对文本进行建模,从而发现文本隐藏的主题结构。在LDA模型中,每个文档都可以由多个主题组合而成,每个主题也可以由多个单词组成。LDA模型可视化可以帮助我们更好地理解文本数据中的主题结构。 有多种方法可以对LDA模型进行可视化,其中一种常用的方法是使用pyLDAvis库。pyLDAvis库可以生成一个交互式的可视化界面,其中包括主题的分布情况、单词的分布情况、主题之间的相似性等信息,使得用户可以更加直观地理解模型的结果。 下面是使用pyLDAvis库对LDA模型进行可视化的示例代码: ``` python import pyLDAvis.gensim import gensim # 导入LDA模型和语料库 lda_model = gensim.models.ldamodel.LdaModel.load('lda_model.model') corpus = gensim.corpora.MmCorpus('corpus.mm') # 对模型进行可视化 vis_data = pyLDAvis.gensim.prepare(lda_model, corpus, dictionary=lda_model.id2word) pyLDAvis.display(vis_data) ``` 这段代码中,我们首先导入了pyLDAvis和gensim库,然后加载了LDA模型和语料库。最后,调用pyLDAvis.gensim.prepare()函数对模型进行可视化,并使用pyLDAvis.display()函数将结果显示在交互式界面中。 需要注意的是,可视化结果只是对模型的一种解释,不一定反映了数据的真实情况。因此,在使用可视化工具时需要慎重分析结果,同时结合其他方法进行验证。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 18
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值