Python Gensim LDA中的文档主题分发

Python Gensim LDA中的文档主题分发,python,lda,gensim,Python,Lda,Gensim,我使用玩具语料库导出了一个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', 'Syst

我使用玩具语料库导出了一个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)的主题从输出中被省略

我想知道这种行为是否是出于美学考虑?我怎样才能得到质量剩余概率在所有其他主题上的分布呢


谢谢你友好的回答

阅读,发现概率小于阈值的主题被忽略。此阈值的默认值为0.01。

我意识到这是一个老问题,但如果有人偶然发现,这里有一个解决方案(问题实际上是
最小概率
参数设置为
LdaModel
,但可能您运行的是较旧版本的gensim)

定义新函数(仅从源代码复制)


上述函数不会根据概率过滤输出主题,但会输出所有主题。如果你不需要<代码>(TopiSigID,value)<代码>元组,而只是值,只需返回<代码> TopICyDIST而不是列表理解(它也会快得多)。

可能其他主题的百分比太低,不能被认为是突出的。我也遇到了同样的问题。你找到解决方法了吗?你能告诉我你是如何创建“语料库”的吗?嗨,gamma是主题的概率分布吗?抱歉,如果这听起来很愚蠢,我对LDA的内部结构不是很熟悉。因为文档内容是:“给定一个稀疏文档向量块,估计该块中每个文档的gamma(控制主题权重的参数)。”。我认为gensim提供了一个生成器(lda[corpus])。
gamma
是每个文档的未规范主题分数,
topic\u dist
是概率分布。是
gensim
提供了一个生成器
lda[corpus]
,该生成器在内部使用
lda.inference
。正如我上面所说的,如果您不需要
(topic\u id,probability)
对,那么自己调用
.interference
会更快。如果您的语料库非常大且不适合内存,您可能需要执行分块,
lda[corpus]
也会在内部进行分块。NB使用以下方法规范所有主题的分布,而不仅仅是第一个主题的分布\u dist=gamma/gamma.sum(axis=1)[:,None]
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)]