python-3.x - 如何获取gensim LDA中所有文档的document_topics分布?

标签 python-3.x gensim lda topic-modeling probability-distribution

我是 python 新手,我需要构建一个 LDA 项目。做了一些预处理步骤后,这是我的代码:

dictionary = Dictionary(docs)
corpus = [dictionary.doc2bow(doc) for doc in docs]

from gensim.models import LdaModel
num_topics = 10
chunksize = 2000
passes = 20
iterations = 400
eval_every = None
temp = dictionary[0]
id2word = dictionary.id2token
model = LdaModel(corpus=corpus, id2word=id2word, chunksize=chunksize, \
                       alpha='auto', eta='auto', \
                       random_state=42, \
                       iterations=iterations, num_topics=num_topics, \
                       passes=passes, eval_every=eval_every)

我想获得文档的主题分布,所有文档并获得主题分布的 10 概率,但是当我使用:
get_document_topics = model.get_document_topics(corpus)
print(get_document_topics)

输出只出现
<gensim.interfaces.TransformedCorpus object at 0x000001DF28708E10>

如何获得文档的主题分布?

最佳答案

函数get_document_topics以 BOW 格式输入单个文档。您在完整的语料库(文档数组)上调用它,因此它返回一个可迭代对象,其中包含每个文档的分数。

你有几个选择。如果您只需要一个文档,请在您想要其值的文档上运行它:

get_document_topics = model.get_document_topics(corpus[0])

或执行以下操作以获取所有文档的分数数组:
get_document_topics = [model.get_document_topics(item) for item in corpus]

或者直接从原始代码访问每个对象:
get_document_topics = model.get_document_topics(corpus)
print(get_document_topics[0])

关于python-3.x - 如何获取gensim LDA中所有文档的document_topics分布?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53313575/

相关文章:

python - 如何使用带有 python 的 Selenium 下载 HTML 网页?

python - 将 gensim 的 Word2Vec 与自定义单词上下文对结合使用

python-3.x - 如何获得 word2vec 词汇表中不存在的单词的向量?

python - 生产环境中的文档相似度

python - AttributeError: 'module' 对象没有属性 '__version__'

r - lda.collapsed.gibbs.sampler 模型和热门词排名

python - 属性错误 : module 'regex' has no attribute 'Pattern'

python-3.x - 错误: No matching distribution found for Cython

python - 在特定时期内为特定 Id 在多个文件之间进行绘图

python - 了解 Gensim 中的 LDA 转换语料库