python - 如何从 gensim 打印 LDA 主题模型? Python

标签 python nlp lda topic-modeling gensim

使用 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 <module>
    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

最佳答案

经过一番折腾,ldamodelprint_topics(numoftopics) 似乎有一些错误。所以我的解决方法是使用 print_topic(topicid):

>>> print lda.print_topics()
None
>>> for i in range(0, lda.num_topics-1):
>>>  print lda.print_topic(i)
0.083*response + 0.083*interface + 0.083*time + 0.083*human + 0.083*user + 0.083*survey + 0.083*computer + 0.083*eps + 0.083*trees + 0.083*system
...

关于python - 如何从 gensim 打印 LDA 主题模型? Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15016025/

相关文章:

nlp - (a^p )(b^q) 是正则语言吗

python scikit学习,在LDA中获取每个主题的文档

python - pyLDAvis : Validation error on trying to visualize topics

python - 添加约束 CPLEX Python API

python - 最小化 Tensorflow 中一个变量的函数

python - 使用 spaCy 为 URL 定制标签和词法

nlp - 潜在狄利克雷分配与文档聚类之间的关系

python - 微服务和多个数据库

python - 如何进行时间序列向后重采样,例如从最后数据日期开始的 5 个工作日?

algorithm - 如何捕获以动词开头并以名词结尾的句子的一部分