- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 python 中的 gensim 包为一些文本文件创建了一个 LDA 模型。我想获得学习模型的主题分布。 gensim ldamodel 类中是否有任何方法或解决方案可以从模型中获取主题的分布?例如,我使用一致性模型来查找具有最佳一致性值的模型,该模型受主题数量在 1 到 5 范围内的影响。获得最佳模型后,我使用 get_document_topics 方法(感谢 kenhbs )来获取主题分布用于创建模型的文档。
id2word = corpora.Dictionary(doc_terms)
bow = id2word.doc2bow(doc_terms)
max_coherence = -1
best_lda_model = None
for num_topics in range(1, 6):
lda_model = gensim.models.ldamodel.LdaModel(corpus=bow, num_topics=num_topics)
coherence_model = gensim.models.CoherenceModel(model=lda_model, texts=doc_terms,dictionary=id2word)
coherence_value = coherence_model.get_coherence()
if coherence_value > max_coherence:
max_coherence = coherence_value
best_lda_model = lda_model
最好的有 4 个主题
print(best_lda_model.num_topics)
4
但是当我使用 get_document_topics 时,我得到的文档分布值少于 4 个。
topic_ditrs = best_lda_model.get_document_topics(bow)
print(len(topic_ditrs))
3
我的问题是:对于一个文档有 4 个主题(使用一致性模型)的最佳 lda 模型,为什么 get_document_topics 对于同一文档返回更少的主题?为什么有些主题的分布非常小(小于 1-e8)?
最佳答案
来自the documentation ,您可以使用两种方法来实现此目的。
如果您的目标是获取特定主题中的主要术语,请使用 get_topic_terms
:
from gensim.model.ldamodel import LdaModel
K = 10
lda = LdaModel(some_corpus, num_topics=K)
lda.get_topic_terms(5, topn=10)
# Or for all topics
for i in range(K):
lda.get_topic_terms(i, topn=10)
您还可以打印整个底层np.ndarray
(在标准 LDA 论文中称为 beta 或 phi,维度为 (K, V) 或 (V, K))。
phi = lda.get_topics()
编辑:从我在原始答案中包含的链接:如果您正在寻找文档的主题分布,请使用
res = lda.get_document_topics(bow)
从文档中可以看出,生成的对象包含以下三个列表:
list of (int, float) – Topic distribution for the whole document. Each element in the list is a pair of a topic’s id, and the probability that was assigned to it.
list of (int, list of (int, float), optional – Most probable topics per word. Each element in the list is a pair of a word’s id, and a list of topics sorted by their relevance to this word. Only returned if per_word_topics was set to True.
list of (int, list of float), optional – Phi relevance values, multipled by the feature length, for each word-topic combination. Each element in the list is a pair of a word’s id and a list of the phi values between this word and each topic. Only returned if per_word_topics was set to True.
现在,
tops, probs = zip(*res[0])
probs
将包含 K(对您来说是 4)个概率。有些可能为零,但它们的总和应该为 1
关于gensim - 从gensim LDA模型中提取主题分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52077016/
我刚刚开始阅读有关潜在狄利克雷分配 LDA 的内容,并希望将其应用到我的项目中。 请问 LDA 是否能够将一个主题分配给多个单词? 例如,文章A谈论“河岸”,而文章B谈论“银行在金融中的作用”。因此,
我刚刚开始阅读有关 Latent Dirichlet Allocation LDA 的内容,并想将其应用到我的项目中。 我可以知道 LDA 是否能够将一个主题分配给多个单词吗? 例如,A 条谈到“河岸
情况: 我有一个 numpy 术语文档矩阵 例如:[[0,1,0,0....],....[......0,0,0,0]]。 我已将上述矩阵插入到 gensim 的 ldamodel 方法中。并且使用
我正在尝试使用 gensim's lda模型。如果我用给定的语料库创建 lda 模型,然后我想用一个新的语料库更新它,其中包含在第一个语料库中看不到的单词,我该怎么做?当我尝试调用 lda_model
library(MASS) example(lda) plot(z) 如何访问 z 中的所有点?我想知道 LD1 和 LD2 上每个点的值,具体取决于它们的 Sp (c,s,v)。 最佳答案 您正在寻
我在 R 中制作了一个 LDA 主题模型,使用 textmineR 包,如下所示。 ## get textmineR dtm dtm2 2] dtm2 2] ###################
我想知道更多关于LDA模型中是否有设置超参数alpha和theta的规则。我运行图书馆提供的 LDA 模型 gensim : ldamodel = gensim.models.ldamodel.Lda
我认为在 LDA 模型中,这些是使用现有模型推断新文档的两种方法。这两种方法有什么区别? 最佳答案 我做了一些测试,我的 ldamodel 有 8 个主题,这里是我的结果:2 个预测主题的文档: li
在阅读有关使用 python 的 LinearDiscriminantAnalysis 的过程中,我有两种不同的方法来实现它,可在此处获得, http://scikit-learn.org/stabl
我有一个通过 Java 中的 Mallet 训练的 LDA 模型。 Mallet LDA 模型生成了三个文件,这使我能够从文件运行模型并推断新文本的主题分布。 现在我想实现一个 Python 工具,它
我正在将 MLlib LDA example 应用于从 enter link description here 下载的各种语料库我正在过滤掉停用词,并排除非常频繁的术语和非常罕见的术语。问题是我总是有
我正在尝试了解潜在狄利克雷分配(LDA)。我有机器学习和概率论的基础知识,并基于这篇博文 http://goo.gl/ccPvE我能够开发 LDA 背后的直觉。但是,我仍然没有完全了解其中的各种计算。
我正在寻找一些适用于大型数据集的主题建模工具。 我当前的训练数据集是 30 GB。我试过 MALLET topic modeling ,但我总是得到 OutOfMemoryError。 如果您有任何提
我正在使用“topicmodels”包在 R 中构建一些主题模型。在预处理并创建文档术语矩阵后,我正在应用以下 LDA Gibbs 模型。这可能是一个简单的答案,但我是 R 的新手,所以就这样吧。有没
我正在尝试为线性判别分析 (LDA) 创建双标图。我正在使用从这里获得的代码的修改版本 https://stats.stackexchange.com/questions/82497/can-the-
是否可以查找某个主题(由 LDA 确定)内的文本? 我有一个包含 5 个主题的列表,每个主题有 10 个单词,是使用 lda 找到的。 我分析了数据框列中的文本。我想选择/过滤某个特定主题中的行/文本
给定一个标准 LDA 模型,其中包含 1000 个主题和数百万个文档,并使用 Mallet/折叠吉布斯采样器进行训练: 在推断新文档时:为什么不直接跳过采样并简单地使用模型的术语主题计数来确定新文档的
我读过 LDA,并且了解当一个人输入一组文档时如何生成主题的数学原理。 引用文献称,LDA 是一种算法,只要给定一个文档集合(无需任何监督),就可以揭示该集合中的文档所表达的“主题”。因此,通过使用
我希望将交叉验证应用于 LDA 算法以确定主题的数量(K)。 我的疑问是关于评估者,因为我希望使用对数似然。创建交叉验证时,我在 .setEvaluator(????) 上设置了什么? // Defi
我希望使用 LDA 将每个文档分配给一个主题。现在我意识到你得到的是来自 LDA 的主题分布。然而,正如您从下面的最后一行中看到的那样,我将其分配给了最可能的主题。 我的问题是这样的。我必须第二次运行
我是一名优秀的程序员,十分优秀!