lda主题模型python实现篇_如何打印gensim中的LDA主题模型?Python

本文介绍如何在Python中使用gensim库处理LDA主题模型。通过示例代码,展示了如何创建和应用LDA模型,并解决在尝试打印LDA模型主题时遇到的TypeError问题。代码中演示了从文档集合中构建词汇表、删除停用词、应用LSA模型,然后构建LDA模型。最后,解释了如何正确地迭代并打印LDA模型的主题。
摘要由CSDN通过智能技术生成

使用gensim我能够从LSA中的一组文档中提取主题,但是如何访问从LDA模型生成的主题?

当打印lda.print_topics(10)时,代码给出了以下错误,因为print_topics()返回了NoneType:Traceback (most recent call last):

File "/home/alvas/workspace/XLINGTOP/xlingtop.py", line 93, in

for top in lda.print_topics(2):

TypeError: 'NoneType' object is not iterable

代码:from gensim import corpora, models, similarities

from gensim.models import hdpmodel, ldamodel

from itertools import izip

documents = ["Human machine interface for lab abc computer applications",

"A survey of user opinion of computer system response time",

"The EPS user interface management system",

"System and human system engineering testing of EPS",

"Relation of user perceived response time to error measurement",

"The generation of random binary unordered trees",

"The intersection graph of paths in trees",

"Graph minors IV Widths of trees and well quasi ordering",

"Graph minors A survey"]

# remove common words and tokenize

stoplist = set('for a of the and to in'.split())

texts = [[word for word in document.lower().split() if word not in stoplist]

for document in documents]

# remove words that appear only once

all_tokens = sum(texts, [])

tokens_once = set(word for word in set(all_tokens) if all_tokens.count(word) == 1)

texts = [[word for word in text if word not in tokens_once]

for text in texts]

dictionary = corpora.Dictionary(texts)

corpus = [dictionary.doc2bow(text) for text in texts]

# I can print out the topics for LSA

lsi = models.LsiModel(corpus_tfidf, id2word=dictionary, num_topics=2)

corpus_lsi = lsi[corpus]

for l,t in izip(corpus_lsi,corpus):

print l,"#",t

print

for top in lsi.print_topics(2):

print top

# I can print out the documents and which is the most probable topics for each doc.

lda = ldamodel.LdaModel(corpus, id2word=dictionary, num_topics=50)

corpus_lda = lda[corpus]

for l,t in izip(corpus_lda,corpus):

print l,"#",t

print

# But I am unable to print out the topics, how should i do it?

for top in lda.print_topics(10):

print top

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值