tkj*_*kja 8 python gensim topic-modeling
该ldamodel在gensim有两种方法:get_document_topics和get_term_topics.
尽管他们在这个gensim教程笔记本中使用,我还不完全理解如何解释输出get_term_topics并创建下面的自包含代码来显示我的意思:
from gensim import corpora, models
texts = [['human', 'interface', 'computer'],
['survey', 'user', 'computer', 'system', 'response', 'time'],
['eps', 'user', 'interface', 'system'],
['system', 'human', 'system', 'eps'],
['user', 'response', 'time'],
['trees'],
['graph', 'trees'],
['graph', 'minors', 'trees'],
['graph', 'minors', 'survey']]
# build the corpus, dict and train the model
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
model = models.ldamodel.LdaModel(corpus=corpus, id2word=dictionary, num_topics=2,
random_state=0, chunksize=2, passes=10)
# show the topics
topics = model.show_topics()
for topic in topics:
print topic
### (0, u'0.159*"system" + 0.137*"user" + 0.102*"response" + 0.102*"time" + 0.099*"eps" + 0.090*"human" + 0.090*"interface" + 0.080*"computer" + 0.052*"survey" + 0.030*"minors"')
### (1, u'0.267*"graph" + 0.216*"minors" + 0.167*"survey" + 0.163*"trees" + 0.024*"time" + 0.024*"response" + 0.024*"eps" + 0.023*"user" + 0.023*"system" + 0.023*"computer"')
# get_document_topics for a document with a single token 'user'
text = ["user"]
bow = dictionary.doc2bow(text)
print "get_document_topics", model.get_document_topics(bow)
### get_document_topics [(0, 0.74568415806946331), (1, 0.25431584193053675)]
# get_term_topics for the token user
print "get_term_topics: ", model.get_term_topics("user", minimum_probability=0.000001)
### get_term_topics: [(0, 0.1124525558321441), (1, 0.006876306738765027)]
Run Code Online (Sandbox Code Playgroud)
因为get_document_topics,输出是有道理的.两个概率加起来为1.0,而user具有更高概率(来自model.show_topics())的主题也具有更高的概率.
但是get_term_topics,有问题:
user具有更高概率(来自model.show_topics())的主题也分配了更高的数字,这个数字意味着什么?get_term_topics,什么时候get_document_topics可以提供(看似)相同的功能并具有有意义的输出?我正在研究LDA主题建模,并发现了这篇文章.我确实创建了两个主题,比如topic1和topic2.
每个主题的前10个单词如下:
0.009*"would" + 0.008*"experi" + 0.008*"need" + 0.007*"like" + 0.007*"code" + 0.007*"work" + 0.006*"think" + 0.006*"make" + 0.006*"one" + 0.006*"get
0.027*"ierr" + 0.018*"line" + 0.014*"0.0e+00" + 0.010*"error" + 0.009*"defin" + 0.009*"norm" + 0.006*"call" + 0.005*"type" + 0.005*"de" + 0.005*"warn
最后,我拿了1个文件来确定最接近的主题.
for d in doc:
bow = dictionary.doc2bow(d.split())
t = lda.get_document_topics(bow)
Run Code Online (Sandbox Code Playgroud)
而输出是[(0, 0.88935698141006414), (1, 0.1106430185899358)].
要回答你的第一个问题,概率都加起来1.0为一个文件,这是get_document_topics做什么.该文档明确指出它返回给定文档弓的主题分布,作为(topic_id,topic_probability)2元组的列表.
此外,我尝试使用get_term_topics作为关键字"ierr"
t = lda.get_term_topics("ierr", minimum_probability=0.000001)结果只是[(1, 0.027292299843400435)]确定每个主题的词贡献,这是有道理的.
因此,您可以根据使用get_document_topics获得的主题分布来标记文档,并且可以基于get_term_topics给出的贡献来确定单词的重要性.
我希望这有帮助.