Gensim LDA中的文档主题分布

17
我使用一个玩具语料库构建了一个LDA主题模型,具体步骤如下:
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']

texts = [[word for word in document.lower().split()] for document in documents]
dictionary = corpora.Dictionary(texts)

id2word = {}
for word in dictionary.token2id:    
    id2word[dictionary.token2id[word]] = word

我发现当我使用少量主题推导模型时,Gensim会为测试文档提供所有潜在主题的主题分布完整报告。例如:
test_lda = LdaModel(corpus,num_topics=5, id2word=id2word)
test_lda[dictionary.doc2bow('human system')]

Out[314]: [(0, 0.59751626959781134),
(1, 0.10001902477790173),
(2, 0.10001375856907335),
(3, 0.10005453508763221),
(4, 0.10239641196758137)]

然而,当我使用大量主题时,报告不再完整。
test_lda = LdaModel(corpus,num_topics=100, id2word=id2word)

test_lda[dictionary.doc2bow('human system')]
Out[315]: [(73, 0.50499999999997613)]

在我的观察中,输出结果中概率小于某个阈值(我观察到具体为0.01)的主题被省略了。

我想知道这种行为是否出于美学考虑?还有,我该如何获取所有其他主题上的概率质量残差分布?

谢谢您的友好回答!


其他主题的百分比可能太低,无法视为显著。 - alvas
我也遇到了同样的问题。您找到了任何解决办法吗? - Shashank
你能告诉我你是如何创建“语料库”的吗? - MysticForce
2个回答

8

阅读源代码,发现小于一个阈值的主题将被忽略。这个默认的阈值为0.01。


8
我知道这是一个老问题,但万一有人遇到了这个问题,在这里提供一个解决方案(实际上该问题已经在当前开发分支中使用LdaModelminimum_probability参数进行了修复,但也许你正在运行旧版本的gensim)。
定义一个新函数(这只是从源码复制过来的)。
def get_doc_topics(lda, bow):
    gamma, _ = lda.inference([bow])
    topic_dist = gamma[0] / sum(gamma[0])  # normalize distribution
    return [(topicid, topicvalue) for topicid, topicvalue in enumerate(topic_dist)]

上述函数输出所有主题而不基于概率筛选,如果您不需要(topic_id, value)元组,只需要值,请返回topic_dist而不是列表推导式(速度更快)。


嗨,伽马是主题的概率分布吗?如果这听起来很傻,我对LDA的内部不是很熟悉。因为文档中写道:“给定一组稀疏的文档向量,估计该组中每个文档的伽马(控制主题权重的参数)。” 我想推断gensim提供了一个生成器(lda [corpus])。 - Shashank
1
gamma 是每个文档的未标准化主题分数,topic_dist 是概率分布。是的,gensim 提供了一个生成器 lda[corpus],该生成器在内部使用了 lda.inference。正如我上面所说,如果你 不需要 (topic_id, probability) 对,那么自己调用 .inference 会更快。如果你的语料库非常大,无法放入内存中,则可能需要执行分块,lda[corpus] 也会在内部执行分块。 - Matti Lyra
1
请使用以下代码对所有主题进行分布归一化,而不仅仅是第一个主题: topic_dist = gamma / gamma.sum(axis=1)[:, None] - aless80

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接