- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我使用 gensim LDA 主题建模从语料库中获取相关主题。现在我想获取代表每个主题的前 20 个文档:在某个主题中概率最高的文档。我想用这种格式将它们保存在一个 CSV 文件中:主题 ID、主题词、主题中每个词的概率、每个主题的前 20 个文档的 4 列。
我已经尝试过 get_document_topics,我认为这是完成这项任务的最佳方法:
all_topics = lda_model.get_document_topics(语料库,minimum_probability=0.0,per_word_topics=False)
但我不确定如何获取最能代表主题的前 20 个文档并将它们添加到 CSV 文件中。
data_words_nostops = remove_stopwords(processed_docs)
# Create Dictionary
id2word = corpora.Dictionary(data_words_nostops)
# Create Corpus
texts = data_words_nostops
# Term Document Frequency
corpus = [id2word.doc2bow(text) for text in texts]
# Build LDA model
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
id2word=id2word,
num_topics=20,
random_state=100,
update_every=1,
chunksize=100,
passes=10,
alpha='auto',
per_word_topics=True)
pprint(lda_model.print_topics())
#save csv
fn = "topic_terms5.csv"
if (os.path.isfile(fn)):
m = "a"
else:
m = "w"
num_topics=20
# save topic, term, prob data in the file
with open(fn, m, encoding="utf8", newline='') as csvfile:
fieldnames = ["topic_id", "term", "prob"]
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
if (m == "w"):
writer.writeheader()
for topic_id in range(num_topics):
term_probs = lda_model.show_topic(topic_id, topn=6)
for term, prob in term_probs:
row = {}
row['topic_id'] = topic_id
row['prob'] = prob
row['term'] = term
writer.writerow(row)
最佳答案
首先,每个文档都有一个主题向量,一个看起来像这样的元组列表:
[(0, 3.0161273e-05), (1, 3.0161273e-05), (2, 3.0161273e-05), (3, 3.0161273e-05), (4,
3.0161273e-05), (5, 0.06556476), (6, 0.14744747), (7, 3.0161273e-05), (8, 3.0161273e-
05), (9, 3.0161273e-05), (10, 3.0161273e-05), (11, 0.011416071), (12, 3.0161273e-05),
(13, 3.0161273e-05), (14, 3.0161273e-05), (15, 0.057074558), (16, 3.0161273e-05),
(17, 3.0161273e-05), (18, 3.0161273e-05), (19, 3.0161273e-05), (20, 0.7178939), (21,
3.0161273e-05), (22, 3.0161273e-05), (23, 3.0161273e-05), (24, 3.0161273e-05)]
其中,例如 (0, 3.0161273e-05),0 是主题 ID,3.0161273e-05 是概率。
#Create a dictionary, with topic ID as the key, and the value is a list of tuples
(docID, probability of this particular topic for the doc)
topic_dict = {i: [] for i in range(20)} # Assuming you have 20 topics.
#Loop over all the documents to group the probability of each topic
for docID in range(num_docs):
topic_vector = lda_model[corpus[docID]]
for topicID, prob in topic_vector:
topic_dict[topicID].append((docID, prob))
#Then, you can sort the dictionary to find the top 20 documents:
for topicID, probs in topic_dict.items():
doc_probs = sorted(probs, key = lambda x: x[1], reverse = True)
docs_top_20 = [dp[0] for dp in doc_probs[:20]]
您将获得每个主题的主题 20 文档。您可以收集一个列表(这将是一个列表列表)或一个字典,以便将它们输出。
关于python - 应用gensim LDA主题建模后,如何获取每个主题概率最高的文档并将其保存在csv文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56408849/
我刚刚开始阅读有关潜在狄利克雷分配 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 的主题分布。然而,正如您从下面的最后一行中看到的那样,我将其分配给了最可能的主题。 我的问题是这样的。我必须第二次运行
我是一名优秀的程序员,十分优秀!