+ V& R2 w( t$ J* `) o" h4 ?- ]; l6 i/ g
四、Running in Python; r! |: W- _* r j" a
准备文档集合; ^) p7 ?" T0 D, ?' T: R
doc1 = "Sugar is bad to consume. My sister likes to have sugar, but not my father." 3 n4 a! R2 n" }& W$ l: h! R, Fdoc2 = "My father spends a lot of time driving my sister around to dance practice."% G' J! a3 c6 X D3 b$ F
doc3 = "Doctors suggest that driving may cause increased stress and blood pressure."7 T& M' B( w* Z T1 ~
doc4 = "Sometimes I feel pressure to perform well at school, but my father never seems to drive my sister to do better." " \+ T+ n |# z& f- E: L5 y; }- Idoc5 = "Health experts say that Sugar is not good for your lifestyle."; |- K+ \$ J6 y4 n- B3 F, y
" m2 @; _/ G) V- g+ \7 Y
# 整合文档数据5 L% u0 K5 x7 S0 S3 `" H4 u+ F
doc_complete = [doc1, doc2, doc3, doc4, doc5]9 A# b8 R' c8 T9 x
% p; N0 v Y* b数据清洗和预处理 " U5 f# M4 T, R8 d! w3 W数据清洗对于任何文本挖掘任务来说都非常重要,在这个任务中,移除标点符号,停用词和标准化语料库(Lemmatizer,对于英文,将词归元)。 ) u- S, J j. W. Z8 [5 | l" H2 x9 j7 S . v& b( c( d. z3 v, T1 Afrom nltk import stopwords : @+ J3 i) D; o- Jfrom nltk.stem.wordnet import WordNetLemmatizer , A! H& _7 i/ t/ m3 i& Timport string. ^+ L- D. j3 o8 ^0 p
; ^1 z8 t4 ~" T8 z# o% I
stop = set(stopwords.words('english'))7 ?- H$ F1 w1 R) Q
exclude = set(string.punctuation) & A: c* R$ k1 }& }% wlemma = WordNetLemmatizer() + h0 D# c7 h+ y 8 \% K5 U/ _- C6 Z# g- Zdef clean(doc): $ Z5 J( D7 j" x3 t7 V stop_free = " ".join([i for i in doc.lower().split() if i not in stop])$ }) }1 ?! F9 R3 v
punc_free = ''.join(ch for ch in stop_free if ch not in exclude) ' d& q7 V/ X/ R: z normalized = " ".join(lemma.lemmatize(word) for word in punc_free.split())! ], E" ^0 j8 M8 Y& J
return normalized7 @ ]) c2 e8 h2 Z- q8 x) F
* D% s; A: m. |1 O' F1 k& ldoc_clean = [clean(doc).split() for doc in doc_complete]* o! D8 `# P* y8 d2 G6 ?1 ?! \9 o
8 o+ K7 @% R: g0 r" ^: w
准备 Document - Term 矩阵7 X2 ]- d! {. {2 M1 X' ^
语料是由所有的文档组成的,要运行数学模型,将语料转化为矩阵来表达是比较好的方式。LDA 模型在整个 DT 矩阵中寻找重复的词语模式。Python 提供了许多很好的库来进行文本挖掘任务,“genism” 是处理文本数据比较好的库。下面的代码掩饰如何转换语料为 Document - Term 矩阵:6 S9 x1 p4 l7 R. U! c$ u
6 V1 P( {, H' d' ^. C2 d* Himport genism , H# V7 z3 \. B8 u5 u: @4 Gfrom gensim import corpora/ t$ s4 h8 F! T- T) @7 F
; x, E2 ~/ _- R; h; [
# 创建语料的词语词典,每个单独的词语都会被赋予一个索引 ; y9 m6 l# j1 f( Xdictionary = corpora.Dictionary(doc_clean)' }. h0 A) z: |# T/ F0 e* Y
( A4 X3 O5 I% l2 Z
# 使用上面的词典,将转换文档列表(语料)变成 DT 矩阵 . t8 p% E* u D X: ` Zdoc_term_matrix = [dictionary.doc2bow(doc) for doc in doc_clean]4 G' o1 y, v# o! K0 Y8 ]! P5 o
/ ^: W- ^) y. z* m
构建 LDA 模型9 ~1 ]% m7 A5 D6 K4 t
创建一个 LDA 对象,使用 DT 矩阵进行训练。训练需要上面的一些超参数,gensim 模块允许 LDA 模型从训练语料中进行估计,并且从新的文档中获得对主题分布的推断。3 D, y( B: r1 A# k7 P. A
( x1 Z/ G5 J' v4 ~9 }1 Y6 h2 G! }
# 使用 gensim 来创建 LDA 模型对象) L k. t. Y6 i# U p$ Y
Lda = genism.models.ldamodel.LdaModel - N1 M: x g8 t2 k. P6 [6 e& f. ^/ [' W( I6 o
# 在 DT 矩阵上运行和训练 LDA 模型; y( O0 b$ P {* p
ldamodel = Lda(doc_term_matrix, num_topics=3, id2word = dictionary, passes=50) ! ^6 P" V+ o' @7 F* C % M& q5 c% n, w# o' M6 d结果 ! \: W" A, e: F. K# 输出结果6 J2 s: D' ^& Q7 D7 F
print(ldamodel.print_topics(num_topics=3, num_words=3))) c0 p" U* Q( F1 L! ^/ O