% p& L4 ~# D1 p' Ffile_object2=open(PATH,encoding = 'utf-8',errors = 'ignore').read().split('\n') #一行行的读取内容 2 R( d+ o5 |+ H. O; @0 x0 o' D5 Z* adata_set=[] #建立存储分词的列表! D% g* |; b. Z6 q0 q
for i in range(len(file_object2)): 8 j8 B! r6 T/ h6 D# `7 v! r result=[] / y3 R6 `! ?9 Z8 T seg_list = file_object2.split() 4 m, G/ ?' z. U5 { for w in seg_list :#读取每一行分词8 G" [: z. S) f* V
result.append(w) $ y$ K+ A3 K3 v. r data_set.append(result) ! B' H' w. b$ B; uprint(data_set) 0 ?, e3 T" O( q8 G8 B* G! A3 z$ a, Y+ @' J$ H
% D0 k( ]7 P6 O$ Q. e$ e6 ldictionary = corpora.Dictionary(data_set) # 构建 document-term matrix ' K) v0 [8 j: n2 Wcorpus = [dictionary.doc2bow(text) for text in data_set] $ v, }5 \" v# R; T/ n+ h#Lda = gensim.models.ldamodel.LdaModel # 创建LDA对象 / Q! ]0 ^6 }% ^ x- m8 `8 ~$ l& J# E4 C% ? s
#计算困惑度 + c2 Z$ i5 x/ t4 ^5 Qdef perplexity(num_topics): $ E) U. z7 s+ L ldamodel = LdaModel(corpus, num_topics=num_topics, id2word = dictionary, passes=30)% h# j/ Y6 O- Y7 c) o8 j% h
print(ldamodel.print_topics(num_topics=num_topics, num_words=15))3 `+ L" w% [: ~& X4 X7 V$ A/ k
print(ldamodel.log_perplexity(corpus))7 a" u+ z9 Y) i
return ldamodel.log_perplexity(corpus) - Q- z1 F( a! h1 M) K- J * |9 [ f; j! j#计算coherence 7 ~& i: |3 \: X# zdef coherence(num_topics): : N2 `+ b9 X8 D" C% n6 } ldamodel = LdaModel(corpus, num_topics=num_topics, id2word = dictionary, passes=30,random_state = 1) ~0 i: y9 y8 |. g k1 j" B+ c' s
print(ldamodel.print_topics(num_topics=num_topics, num_words=10)) ) |3 S9 c' R0 H9 A. d ldacm = CoherenceModel(model=ldamodel, texts=data_set, dictionary=dictionary, coherence='c_v') 0 V& E$ m) p/ h/ [9 i1 C print(ldacm.get_coherence())5 b) m4 M4 T% ?3 T
return ldacm.get_coherence()' x& H+ G+ U# N, \' ]
- i; }% [2 i1 W+ a# 绘制困惑度折线图0 N, ~% C, ~( F7 Z; I, H, G
x = range(1,15) " d/ K! S5 M/ o6 r! s9 z4 V# B# z = [perplexity(i) for i in x]' O" g, t% U$ J: A
y = [coherence(i) for i in x]% g) ]9 |5 B# _1 K7 T
plt.plot(x, y)* \0 `+ u# ]' F/ h, }! R% i
plt.xlabel('主题数目')+ M6 B. p* o. M0 H2 y1 h, { O2 A
plt.ylabel('coherence大小')1 N& A" |; w! u
plt.rcParams['font.sans-serif']=['SimHei']( T( j0 s* U& n0 }" h
matplotlib.rcParams['axes.unicode_minus']=False . X6 d) P7 X, X7 Y- f+ zplt.title('主题-coherence变化情况') 6 q, b" g* x6 l; N. w- w& Xplt.show() : [' O1 c' [( i0 Y/ ~. m) O( v/ T8 L/ ` z* d
from gensim.models import LdaModel5 r$ X: [7 [3 g- V, b
import pandas as pd % U, M- H: t3 g3 p! S; j0 tfrom gensim.corpora import Dictionary ; [, P7 e1 L$ |) O4 S$ Xfrom gensim import corpora, models1 r6 b. S# k0 l! Z& E( s
import csv8 ?- y( l E# w+ b5 `8 v
# }- X' q; e9 o, g' B# 准备数据* {6 {5 S' u5 z; D1 {' B3 _
PATH = "E:/data/output1.csv"( z+ R# E0 z1 y6 M, u1 W, X4 R8 C
2 w3 u/ U' g6 j3 j y- @& W
file_object2=open(PATH,encoding = 'utf-8',errors = 'ignore').read().split('\n') #一行行的读取内容& ^1 s8 @3 ?' m
data_set=[] #建立存储分词的列表 ' [/ G9 |' M# p' Y6 Hfor i in range(len(file_object2)):3 Q( y4 r. J+ U0 K. h- |2 _3 Z5 i
result=[]8 A' V( G2 p" D
seg_list = file_object2.split() 9 W+ f% g/ ^6 W; l for w in seg_list :#读取每一行分词 " K x( }. i" f) u result.append(w)8 J6 l4 v- E' I" T( N
data_set.append(result); @& ~ y1 P1 B" t$ H% {$ {
3 X; p. K4 S3 B- hdictionary = corpora.Dictionary(data_set) # 构建 document-term matrix ) P" v% X4 |/ j' e tcorpus = [dictionary.doc2bow(text) for text in data_set]4 I& e5 }/ ]# K% j" |) Y5 O
* ]. u2 u9 j/ m- O
lda = LdaModel(corpus=corpus, id2word=dictionary, num_topics=5, passes = 30,random_state=1) c$ P* `* i( m+ K" C
topic_list=lda.print_topics()& b7 N) z* d% {. y! T: @" r6 p
print(topic_list) 3 C4 L: i& S! O0 {* y( p9 ?$ M( t" [% M ~) ]
result_list =[] % k. @; I w5 jfor i in lda.get_document_topics(corpus)[:]: 9 c4 f2 O; d. C! {" H- {* R4 J, s listj=[], r) i) C% e5 @' s
for j in i: 1 A' p9 N$ z g, q8 E# h listj.append(j[1]) - v/ f- `* w. X6 z* U+ a6 x! \ bz=listj.index(max(listj))6 u0 f, R0 `3 C; x7 f8 z9 M1 l1 S
result_list.append(i[bz][0]) " z9 N" ^! \4 J" @2 Hprint(result_list) ( h# @- W4 ], F# ]/ \+ U* {+ O& A* P; \! r- ^2 T8 W% j
import pyLDAvis.gensim / O. o4 D8 R/ A) f7 YpyLDAvis.enable_notebook() 3 g% |5 @# I+ U3 B1 {data = pyLDAvis.gensim.prepare(lda, corpus, dictionary) 1 Y* Q. H7 H% P+ XpyLDAvis.save_html(data, 'E:/data/topic.html') * I, z/ ^' ]* X. A: V有需要自取~ + @6 u d% B! \————————————————9 M2 v+ P# {1 T* \2 S
版权声明:本文为CSDN博主「阿丢是丢心心」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。 ; J! z6 t- z/ b原文链接:https://blog.csdn.net/weixin_41168304/article/details/122389948 9 i1 L7 n+ S7 W7 ]3 U" Z. t( F& L$ Q
0 v! D" e7 `" @+ i% L