数学建模社区-数学中国
标题:
LDA主题模型简介及Python实现
[打印本页]
作者:
杨利霞
时间:
2022-9-7 15:38
标题:
LDA主题模型简介及Python实现
LDA主题模型简介及Python实现
/ V' H1 V" b1 d8 _
& R( C7 l5 Q- @ c5 |- I8 ]4 x7 w5 ]
一、LDA主题模型简介
- z/ m0 X; J8 Q- G+ K* O
LDA主题模型主要用于推测文档的主题分布,可以将文档集中每篇文档的主题以概率分布的形式给出根据主题进行主题聚类或文本分类。
0 t+ f* L1 [8 J3 j" T' o: w
$ t/ k9 f- F- Q" v" G3 Z
LDA主题模型不关心文档中单词的顺序,通常使用词袋特征(bag-of-word feature)来代表文档。词袋模型介绍可以参考这篇文章:文本向量化表示——词袋模型 - 知乎
5 `% w3 B* D9 w3 @* [ h( z! u
* ^7 w/ O- P4 z1 Q7 B+ l0 Q5 J
了解LDA模型,我们需要先了解LDA的生成模型,LDA认为一篇文章是怎么形成的呢?
$ R' u. p6 z8 C; p
# {- F. m) {. ? Y
LDA模型认为主题可以由一个词汇分布来表示,而文章可以由主题分布来表示。
( \0 Q4 f- A+ q3 m7 K4 x
; A; N. q- ^3 |, @$ K) p
比如有两个主题,美食和美妆。LDA说两个主题可以由词汇分布表示,他们分别是:
* z1 {( \2 q5 ~8 u1 N# m$ ^1 C
* W2 [& j" Q- v% j: F% a4 _
{面包:0.4,火锅:0.5,眉笔:0.03,腮红:0.07}
+ }8 y/ j- ?- h0 p0 @3 P8 \. B
{眉笔:0.4,腮红:0.5,面包:0.03,火锅:0.07}
( u: A. \( q6 g$ S- Q
" l, f# Q" Y, K# W
同样,对于两篇文章,LDA认为文章可以由主题分布这么表示:
' K* i$ F4 v5 ^" F1 ?5 R: Y1 N
5 G3 S! c7 }7 ~( d. L* h4 ^
《美妆日记》{美妆:0.8,美食:0.1,其他:0.1}
0 ~1 f y7 F- j h% A
; ^$ _$ C3 t& z% f5 c6 o
《美食探索》{美食:0.8,美妆:0.1,其他:0.1}
9 U3 }% n( r( v; x% ]6 _
# {( I( h* Z! B) k+ w
所以想要生成一篇文章,可以先以一定的概率选取上述某个主题,再以一定的概率选取那个主题下的某个单词,不断重复这两步就可以生成最终文章。
2 R% I% R. ?7 E: z. K: i$ C
9 u! P9 N2 o8 h& k7 p2 G5 `' \6 H
在LDA模型中,一篇文档生成的方式如下:
}+ P* u5 t6 N; M& j8 J
1 Y% ]; K* h1 w4 \5 i/ o |
8 d2 f6 l. B' H: B& H
6 L& v$ k+ E9 S* R
其中,类似Beta分布是二项式分布的共轭先验概率分布,而狄利克雷分布(Dirichlet分布)是多项式分布的共轭先验概率分布。
+ u: L; W% z+ A$ d F/ {
; m0 [; b5 Y1 V9 c. l# l6 |
; }. r& G8 q6 u
! |8 N: I- ~* I" F# `0 h
如果我们要生成一篇文档,它里面的每个词语出现的概率为:
0 _. m/ X/ C& V$ p
4 w; i& N$ C L& S( z; {# c. U
# o/ r, U O8 l4 @$ t, [
8 Q. D* \! q3 s! k
更详细的数学推导可以见:通俗理解LDA主题模型_结构之法 算法之道-CSDN博客_lda模型
6 ?8 f o6 r8 M8 L
8 m* N3 a* }. X, w
看到文章推断其隐藏的主题分布,就是建模的目的。换言之,人类根据文档生成模型写成了各类文章,然后丢给了计算机,相当于计算机看到的是一篇篇已经写好的文章。现在计算机需要根据一篇篇文章中看到的一系列词归纳出当篇文章的主题,进而得出各个主题各自不同的出现概率:主题分布。
+ a; L) |" e! U4 c" j1 ~3 `' |
4 d: Q) F. V7 N+ L; a/ J
至于LDA主题模型在计算机中具体是怎么实现的,我们也可以不必细究,现在有很多可以直接用来进行LDA主题分析的包,我们直接用就行。(没错,我就是调包侠)
5 i2 X- o3 u% O1 |9 X5 D+ E' v C$ C; z
E" `$ e' ~$ X! m" }% v
二、Python实现
/ t, v: s4 o# C9 |& t0 `: r
在用Python进行LDA主题模型分析之前,我先对文档进行了分词和去停用词处理(详情可以看我之前的文章:用python对单一微博文档进行分词——jieba分词(加保留词和停用词)_阿丢是丢心心的博客-CSDN博客_jieba 停用词)
% R& p& g! f& Y$ s( v- [6 L8 B$ O
7 [+ [6 y3 M& [9 x- o9 Z
我下面的输入文件也是已经分好词的文件
+ j) ]' l5 U1 i% q m
6 B" M" j" ^2 c! D: R
1.导入算法包
5 D+ a }9 z2 P3 ~
import gensim
& E1 J( }* y, `
from gensim import corpora
3 u$ b0 g* [9 {, l; ^
import matplotlib.pyplot as plt
. r+ e8 J* z g7 y
import matplotlib
1 {6 y$ o4 h/ `' ]0 N: L
import numpy as np
1 ~$ b5 ~. x* h! R+ d- g
import warnings
! k1 p1 B' I6 Q1 v8 _
warnings.filterwarnings('ignore') # To ignore all warnings that arise here to enhance clarity
7 Z8 a5 I9 @' H! A8 h, V9 Q9 x* R: S
2 } f: T$ f9 Y y
from gensim.models.coherencemodel import CoherenceModel
% L& c2 O, v: {2 {. Q: ]
from gensim.models.ldamodel import LdaModel
! ]* B5 X* ~' Q& \1 ]
2.加载数据
& G6 M- U* K# p+ k( B. _
先将文档转化为一个二元列表,其中每个子列表代表一条微博:
+ Y) e2 ]$ j9 X& B+ y
3 b+ n; \4 D+ q, X: G
PATH = "E:/data/output.csv"
}( W' b& D+ e: z+ d K, I
& k% {7 f3 R. U) L6 K$ U. U
file_object2=open(PATH,encoding = 'utf-8',errors = 'ignore').read().split('\n') #一行行的读取内容
8 K3 I0 @- }( n9 {& L/ G& A
data_set=[] #建立存储分词的列表
: K7 Q! F- ^/ \% [% @8 Q: Z7 h' N
for i in range(len(file_object2)):
$ G" ?- P8 `# D6 a7 y- K# |4 V
result=[]
' z6 N- w' Q! e" g3 u8 ~2 l; P
seg_list = file_object2
.split()
; \! Q( x2 b+ x0 B0 d
for w in seg_list : #读取每一行分词
0 C; m9 k F9 y' Y0 F
result.append(w)
# Y; G4 a, W9 z; w4 L. b. Z
data_set.append(result)
# e& `/ a5 W1 F4 \; t1 @
print(data_set)
$ C6 N: o+ _: P1 x. `9 s
构建词典,语料向量化表示:
* E) P! f8 P2 a) ~
T) P3 ^$ L0 }0 N8 F. n
dictionary = corpora.Dictionary(data_set) # 构建词典
1 m* q- D/ _/ O, m0 b' F. l0 b( x
corpus = [dictionary.doc2bow(text) for text in data_set] #表示为第几个单词出现了几次
0 b0 ^2 I/ _+ d# v& k) p% b4 p
3.构建LDA模型
5 c1 S' r2 |$ F$ o3 q; ^
ldamodel = LdaModel(corpus, num_topics=10, id2word = dictionary, passes=30,random_state = 1) #分为10个主题
* j+ e7 ]+ t. f2 R! f
print(ldamodel.print_topics(num_topics=num_topics, num_words=15)) #每个主题输出15个单词
F. N2 b a- e* V$ D T3 V
这是确定主题数时LDA模型的构建方法,一般我们可以用指标来评估模型好坏,也可以用这些指标来确定最优主题数。一般用来评价LDA主题模型的指标有困惑度(perplexity)和主题一致性(coherence),困惑度越低或者一致性越高说明模型越好。一些研究表明perplexity并不是一个好的指标,所以一般我用coherence来评价模型并选择最优主题,但下面代码两种方法我都用了。
0 ?1 a- S/ Q5 \$ L0 x9 j
1 o! Q e: v7 T' N- y5 V- |
#计算困惑度
1 I, N0 r1 {8 ^$ s$ M9 \, Y: P) m
def perplexity(num_topics):
# k0 ?4 f/ G6 [9 D& p% A7 O% ?6 b
ldamodel = LdaModel(corpus, num_topics=num_topics, id2word = dictionary, passes=30)
' t1 s3 K$ r! U* s2 R
print(ldamodel.print_topics(num_topics=num_topics, num_words=15))
; b: i% z" L) E1 l; H+ h" l8 z/ Y" d
print(ldamodel.log_perplexity(corpus))
% y- {: h6 k' i9 i- c% _
return ldamodel.log_perplexity(corpus)
* W- y0 R# x6 S: \% H$ p
#计算coherence
% T6 _* s, @; i5 }9 K
def coherence(num_topics):
|+ S2 `5 Z9 n! R( Z4 [" z
ldamodel = LdaModel(corpus, num_topics=num_topics, id2word = dictionary, passes=30,random_state = 1)
& {0 t0 G/ { k4 u
print(ldamodel.print_topics(num_topics=num_topics, num_words=10))
9 w9 P/ _# K z7 z5 h
ldacm = CoherenceModel(model=ldamodel, texts=data_set, dictionary=dictionary, coherence='c_v')
2 O2 E6 }. n2 n1 Q. o
print(ldacm.get_coherence())
c9 r8 G/ c, [7 f
return ldacm.get_coherence()
4 q% } e- I8 E- N" ?
4.绘制主题-coherence曲线,选择最佳主题数
~% D) G7 T2 Z
x = range(1,15)
) e# o0 E8 i* |, @; m, V! f6 [
# z = [perplexity(i) for i in x] #如果想用困惑度就选这个
8 ^1 `2 ?. \. d1 J6 k7 d5 `* A
y = [coherence(i) for i in x]
4 f' m- R7 U8 A$ r- l+ ~
plt.plot(x, y)
$ g L9 _* |7 m k
plt.xlabel('主题数目')
8 |$ ^6 [' a* {( X* \: r1 U0 ~" H2 ]
plt.ylabel('coherence大小')
" d- v* o) T8 l* t: F/ F
plt.rcParams['font.sans-serif']=['SimHei']
9 }. v1 J; x- X- ^
matplotlib.rcParams['axes.unicode_minus']=False
J. t* a; j' D7 d; A. M8 f2 A. M
plt.title('主题-coherence变化情况')
, X/ J. [ | m9 ~6 H7 O. U8 L8 a
plt.show()
/ v) x6 {8 I- g( r2 x) j2 E) \
最终能得到各主题的词语分布和这样的图形:
- Q/ }9 m1 s ^
( E) S2 B1 A7 v5 T
2 A! `2 k4 k, L4 D
5 W+ C1 w2 C5 D+ I; t1 L8 x
5.结果输出与可视化
: V) s) Z" ]2 R7 M
通过上述主题评估,我们发现可以选择5作为主题个数,接下来我们可以再跑一次模型,设定主题数为5,并输出每个文档最有可能对应的主题
4 |# Q* E2 y/ D6 ]& }
3 ?* E! v: h% R. c6 M
from gensim.models import LdaModel
U: p. z/ H2 v7 k/ ~( J
import pandas as pd
! J! U$ L! D- j6 _5 C
from gensim.corpora import Dictionary
/ i& [( Y0 w2 ^: @0 O
from gensim import corpora, models
' c+ A+ K n1 h7 u" B
import csv
' F9 G/ V9 r( @" O" Y2 k
, u. R" @8 W( Y6 g/ |
# 准备数据
% o% _' ?' g6 O6 A( U2 o( @
PATH = "E:/data/output1.csv"
" k* K1 x# X: {1 r4 T
V/ O8 f& s' _( h! y
file_object2=open(PATH,encoding = 'utf-8',errors = 'ignore').read().split('\n') #一行行的读取内容
1 Z+ ^3 R9 y- N5 o: t
data_set=[] #建立存储分词的列表
5 X( D* _2 v& Q! t2 }$ A/ x$ q
for i in range(len(file_object2)):
/ H. [, H' |8 f; K! x8 E% J
result=[]
0 k& X/ X( I) x$ P* u9 k9 a
seg_list = file_object2
.split()
" }: w) w: {! H2 u
for w in seg_list :#读取每一行分词
/ K: X$ I% F9 W7 E
result.append(w)
8 r3 o0 }! o& P, R' f
data_set.append(result)
% A' m% m# Q0 f; N. {! s1 k
6 l' @2 T9 F$ ~; r
dictionary = corpora.Dictionary(data_set) # 构建词典
' X( U7 O' r- v! ^ h+ ?# }* M6 X8 ?
corpus = [dictionary.doc2bow(text) for text in data_set]
# F1 G$ s" X" z% L8 ~6 Q5 x/ {
; b' _2 r/ _' b. E P
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, passes = 30,random_state=1)
' e4 l8 O. g2 V- e0 d; m" }/ g1 I
topic_list=lda.print_topics()
5 i7 e; b6 c# E, w/ G+ L
print(topic_list)
- `+ h5 p2 u4 d* O5 ?5 x+ u6 D
$ B4 C+ b# z. k" W. L8 m# }
for i in lda.get_document_topics(corpus)[:]:
5 z4 Y/ L# w3 ]: @0 _
listj=[]
1 ?' ] W* k6 R# Z( j
for j in i:
1 E$ t6 ~) E$ }' o
listj.append(j[1])
: a% n# O4 E3 a4 K% [: @
bz=listj.index(max(listj))
* P: q7 z: a- p0 N
print(i[bz][0])
3 X0 K5 l5 Y) x, F9 C8 n
: ?. W6 |, \% C/ \
同时我们可以用pyLDAvis对LDA模型结果进行可视化:
7 O o* T5 g N v
6 c' ?8 m5 D8 I3 L# U, G: W
import pyLDAvis.gensim
7 s7 l1 _* Q9 x# N+ |* V H' {
pyLDAvis.enable_notebook()
5 S3 Y9 [$ a1 |8 K
data = pyLDAvis.gensim.prepare(lda, corpus, dictionary)
4 _9 [4 { t$ G5 Z$ n2 @$ ]/ u7 ?3 A0 g- C
pyLDAvis.save_html(data, 'E:/data/3topic.html')
& t7 c0 Z9 w- Y. {) F
大概能得到这样的结果:
# Y0 t/ f+ \, C4 o$ a
4 T) _3 ?! _6 H! L
2 x# B* ?) e+ J; w" s; U p
% p+ P9 B+ @9 }
左侧圆圈表示主题,右侧表示各个词语对主题的贡献度。
: ]$ f( z1 z J" s+ E4 C/ I- H
% U# |! {, B& R$ Z0 T8 @
所有代码如下:
6 I1 s5 Z. @. d8 s" u5 c7 A$ I# L
import gensim
& y4 B( V0 t# s' E9 z, c
from gensim import corpora
' P; B U1 h2 ~+ E, O0 a* h8 O
import matplotlib.pyplot as plt
. \4 Z. \% S3 n# ]1 P6 p9 N* h
import matplotlib
2 A3 a' B2 D9 d( D
import numpy as np
" k+ J R# m' U- l+ P) s; P/ j+ G
import warnings
6 T$ {! ~9 z! @+ X6 d
warnings.filterwarnings('ignore') # To ignore all warnings that arise here to enhance clarity
* J" {7 U1 I9 V
/ h8 k* g! |3 S, n' q
from gensim.models.coherencemodel import CoherenceModel
. k9 i4 f2 M& V. t
from gensim.models.ldamodel import LdaModel
0 i1 {! `" G/ @0 ] L6 a* G/ `2 f
0 @/ H3 f: Y3 B' v2 x; f: ~3 R
1 I8 y X1 B3 g* w
: y! q% X1 A2 S3 H( H# a
# 准备数据
; E; [. C8 K+ d
PATH = "E:/data/output.csv"
' h3 ~0 w2 l- q* T. e$ P: F
* m. c8 h# V9 a! O7 k1 q1 q
file_object2=open(PATH,encoding = 'utf-8',errors = 'ignore').read().split('\n') #一行行的读取内容
4 q$ c) p0 Y9 Y4 d9 \
data_set=[] #建立存储分词的列表
. m8 y5 D9 D8 W5 J
for i in range(len(file_object2)):
6 O0 }& T7 E" w: j0 Z# @- P# |
result=[]
. Q5 x4 H, X. ^, x) x
seg_list = file_object2
.split()
" }2 r; z0 @6 [5 I3 b* R/ J
for w in seg_list :#读取每一行分词
% X% Q5 }7 Z# c7 K8 ~4 V' P3 }
result.append(w)
* Y) Y! _+ [$ r
data_set.append(result)
# n4 \4 s2 Z t5 D
print(data_set)
% d3 z7 v1 O: ~5 I9 q6 P2 V, M4 B
5 [: I9 K* c# L. E3 k
" r, D; F- r- C( n% h8 W5 [
dictionary = corpora.Dictionary(data_set) # 构建 document-term matrix
% p9 E0 H" p" y7 Y( s9 i( X, m1 |; K: G7 D
corpus = [dictionary.doc2bow(text) for text in data_set]
+ w9 [; o. ?3 \/ `- q3 t3 E& T
#Lda = gensim.models.ldamodel.LdaModel # 创建LDA对象
; c' U( X8 N6 h5 }. R w" U
! I& R$ V4 x. k& X7 [! e( N
#计算困惑度
_4 u5 @9 x- I9 g0 c
def perplexity(num_topics):
5 X8 x) ~3 q+ e; e C6 x0 f) N4 |7 x+ b
ldamodel = LdaModel(corpus, num_topics=num_topics, id2word = dictionary, passes=30)
2 |$ a5 D- [) Q3 X: K0 S
print(ldamodel.print_topics(num_topics=num_topics, num_words=15))
8 Q8 d. \2 m: q x1 Q. d: a) }$ p$ w; c
print(ldamodel.log_perplexity(corpus))
6 |! t) @1 ?0 T$ t" r9 y$ H
return ldamodel.log_perplexity(corpus)
& f- a1 p" a- ?4 h
4 ]1 O6 a! j+ [# C) W! [
#计算coherence
" r& C5 _# B" i5 W
def coherence(num_topics):
4 B& W- J8 N* N$ m3 U
ldamodel = LdaModel(corpus, num_topics=num_topics, id2word = dictionary, passes=30,random_state = 1)
" \. {- K0 r+ M
print(ldamodel.print_topics(num_topics=num_topics, num_words=10))
2 p& }$ |# m" u# r
ldacm = CoherenceModel(model=ldamodel, texts=data_set, dictionary=dictionary, coherence='c_v')
9 b; g" d% I( z4 K3 O( J/ Z
print(ldacm.get_coherence())
1 S6 u+ m" b2 W0 i u0 H
return ldacm.get_coherence()
# l" C& ]$ A/ c
' x5 t; s1 y& ^! m
# 绘制困惑度折线图
' N, m) ]7 q3 c/ q+ t& Q. ~
x = range(1,15)
9 n! Q b: W6 B3 Q/ J' A1 p
# z = [perplexity(i) for i in x]
: n& o' p9 }$ C
y = [coherence(i) for i in x]
& Y( z8 f0 g( ?7 D# O; i2 G$ b
plt.plot(x, y)
. a5 H: }) Z8 S9 Q8 I1 ]
plt.xlabel('主题数目')
0 S+ P" c, k$ i) [9 ?2 |/ p% s! M
plt.ylabel('coherence大小')
; t; [6 ~9 q* Z4 c+ `6 r9 I" q
plt.rcParams['font.sans-serif']=['SimHei']
/ i1 ^! f1 N8 a) k8 M* _
matplotlib.rcParams['axes.unicode_minus']=False
6 Y0 s7 s1 y% R/ U5 m3 m
plt.title('主题-coherence变化情况')
8 |: E9 q" m# o
plt.show()
4 C3 ]+ F7 S" b1 z
1 M+ _9 E% R: C1 Q7 N$ t
from gensim.models import LdaModel
3 k) P' `3 I' [% t
import pandas as pd
* s( F: c' c( F) Q9 ^, z
from gensim.corpora import Dictionary
}# h" v2 L E O) n' v
from gensim import corpora, models
7 R* K. w- n% |
import csv
4 Z8 {3 P- _, ?
/ @; M* [ I' _$ s' Y. d
# 准备数据
/ h( s( c( Z9 W$ t7 w
PATH = "E:/data/output1.csv"
3 e% ]: w4 L% P" t0 t
7 u6 v' t% e/ Q; X0 m e
file_object2=open(PATH,encoding = 'utf-8',errors = 'ignore').read().split('\n') #一行行的读取内容
7 x# L& ?5 S! a( S
data_set=[] #建立存储分词的列表
" J4 }; Z1 G& m
for i in range(len(file_object2)):
7 Q: c: ?! |1 @ H' i! O& v
result=[]
, y X8 q) X: ~
seg_list = file_object2
.split()
5 L+ I q/ D6 J4 W/ y R
for w in seg_list :#读取每一行分词
9 e4 n7 \2 x2 U. S4 u& ^' V# ]
result.append(w)
- [: i8 U# Y) R: q
data_set.append(result)
2 K0 I* E# H2 @3 t5 G6 n: ?
+ O6 v- p- N% v& Y
dictionary = corpora.Dictionary(data_set) # 构建 document-term matrix
5 `1 X+ ]# Z; P
corpus = [dictionary.doc2bow(text) for text in data_set]
6 k3 ^9 h% m6 Z' ^! @- |- o
! n" x0 F2 ?% R, B6 D0 S$ j
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, passes = 30,random_state=1)
8 `2 N! O2 X- u
topic_list=lda.print_topics()
+ v0 u7 X* n. z! U
print(topic_list)
1 u" }) x6 o; J I1 O; t
3 v' G! j4 P9 V1 F. g, a( r
result_list =[]
/ C# R3 O7 z$ w! ~
for i in lda.get_document_topics(corpus)[:]:
$ p0 C4 Y1 m" ]. k3 x, L0 z& o
listj=[]
9 f. L+ A7 j' G) i3 z( _2 g
for j in i:
/ x# P$ Q H( B6 r
listj.append(j[1])
& v6 F |7 {, _4 K9 f' a' p0 U. }
bz=listj.index(max(listj))
: g1 t2 d/ r0 Z3 g( x3 e- T) l
result_list.append(i[bz][0])
/ t4 u' G, \( n6 \
print(result_list)
( s# @$ K8 w+ p6 U1 k0 Y% h
8 F( r! S; g& s `* f% Q- E: E
import pyLDAvis.gensim
8 o& [9 ?1 |* @% \ K+ _
pyLDAvis.enable_notebook()
. m; H) U" q: h" X1 ?
data = pyLDAvis.gensim.prepare(lda, corpus, dictionary)
2 ]8 I% J! f$ ]) Q
pyLDAvis.save_html(data, 'E:/data/topic.html')
5 [3 h7 `6 V" C. m
有需要自取~
! E/ M- R8 ?. U7 n9 ]9 q* P
————————————————
, s- @7 ^1 s$ c5 ^! Y* C/ @& Q
版权声明:本文为CSDN博主「阿丢是丢心心」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
' I% |) i3 K* E1 F. a) S, T7 M
原文链接:https://blog.csdn.net/weixin_41168304/article/details/122389948
- q) M) x* K0 P* ] `7 H
1 C! p/ a' R5 h, H; D# a
, E# m, T( z! r0 ^4 ?7 m$ C( n
欢迎光临 数学建模社区-数学中国 (http://www.madio.net/)
Powered by Discuz! X2.5