python - gensim 中的 get_document_topics 和 get_term_topics

标签 python gensim topic-modeling

gensim中的ldamodel有两个方法:get_document_topicsget_term_topics

尽管在本 gensim 教程 notebook 中使用了它们,但我并不完全理解如何解释 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)]

对于 get_document_topics,输出是有意义的。两个概率加起来为 1.0,user 具有更高概率的主题(来自 model.show_topics())也被分配了更高的概率。

但是对于get_term_topics,有问题:

  1. 概率加起来不等于 1.0,为什么?
  2. 虽然在数字上,user 具有更高概率的主题(来自 model.show_topics())也分配了更高的数字,但这个数字是什么意思?
  3. get_document_topics 可以提供(看似)相同的功能并具有有意义的输出时,我们为什么要使用 get_term_topics

最佳答案

我正在研究 LDA 主题建模并偶然发现了这篇文章。我确实创建了两个主题,比如 topic1 和 topic2。

每个主题的前 10 个词如下: 0.009*“会”+ 0.008*“体验”+ 0.008*“需要”+ 0.007*“喜欢”+ 0.007*“代码”+ 0.007*“工作”+ 0.006*“思考”+ 0.006*“制作” + 0.006*“一个”+ 0.006*“得到”

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)

输出为 [(0, 0.88935698141006414), (1, 0.1106430185899358)]

要回答您的第一个问题,文档的概率总和为 1.0,这就是 get_document_topics 所做的。该文档明确指出它返回给定文档 bow 的主题分布,作为 (topic_id, topic_probability) 2 元组的列表。

此外,我尝试为关键字 "ierr" 获取 get_term_topics

t = lda.get_term_topics("ierr", minimum_probability=0.000001) 结果是 [(1, 0.027292299843400435)] 只不过是单词 contribution for确定每个主题,这是有道理的。

因此,您可以根据使用 get_document_topics 获得的主题分布来标记文档,并且可以根据 get_term_topics 给出的贡献来确定单词的重要性。

希望对您有所帮助。

关于python - gensim 中的 get_document_topics 和 get_term_topics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43357247/

相关文章:

gensim - Word2Vec:使用的窗口大小的影响

java - 如何将命令行训练的主题模型读入Java类?

python - 通过 imp.load_source 加载具有相同名称的模块导致模块合并

javascript - 如何将值从 JS 传回 Python

keras - 嵌入 vs 将词向量直接插入输入层

Python Gensim word2vec 词汇键

gensim - 如何使用gensim的LDA从查询中进行文本检索?

r - 在 R 中使用 LDA 对新文本进行分类

Python:连接四个交替的转弯

python - 使用 Selenium 的下拉菜单和复选框 (python3)