- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 python gensim
从一个包含 231 个句子的小型语料库训练一个 Latent Dirichlet Allocation (LDA) 模型。然而,每次我重复这个过程,它都会产生不同的主题。
为什么相同的LDA参数和语料库每次生成不同的主题?
以及如何稳定话题生成?
我正在使用这个语料库 ( http://pastebin.com/WptkKVF0 ) 和这个停用词列表 ( http://pastebin.com/LL7dqLcj ),这是我的代码:
from gensim import corpora, models, similarities
from gensim.models import hdpmodel, ldamodel
from itertools import izip
from collections import defaultdict
import codecs, os, glob, math
stopwords = [i.strip() for i in codecs.open('stopmild','r','utf8').readlines() if i[0] != "#" and i != ""]
def generateTopics(corpus, dictionary):
# Build LDA model using the above corpus
lda = ldamodel.LdaModel(corpus, id2word=dictionary, num_topics=50)
corpus_lda = lda[corpus]
# Group topics with similar words together.
tops = set(lda.show_topics(50))
top_clusters = []
for l in tops:
top = []
for t in l.split(" + "):
top.append((t.split("*")[0], t.split("*")[1]))
top_clusters.append(top)
# Generate word only topics
top_wordonly = []
for i in top_clusters:
top_wordonly.append(":".join([j[1] for j in i]))
return lda, corpus_lda, top_clusters, top_wordonly
#######################################################################
# Read textfile, build dictionary and bag-of-words corpus
documents = []
for line in codecs.open("./europarl-mini2/map/coach.en-es.all","r","utf8"):
lemma = line.split("\t")[3]
documents.append(lemma)
texts = [[word for word in document.lower().split() if word not in stopwords]
for document in documents]
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
lda, corpus_lda, topic_clusters, topic_wordonly = generateTopics(corpus, dictionary)
for i in topic_wordonly:
print i
最佳答案
Why does the same LDA parameters and corpus generate different topics everytime?
因为 LDA 在训练和推理步骤中都使用了随机性。
And how do i stabilize the topic generation?
通过在每次训练模型或执行推理时将 numpy.random
种子重置为相同的值,使用 numpy.random.seed
:
SOME_FIXED_SEED = 42
# before training/inference:
np.random.seed(SOME_FIXED_SEED)
(这很丑陋,它使 Gensim 结果难以重现;考虑提交补丁。我已经打开了一个 issue。)
关于python - 每次我在同一个语料库上训练时,LDA 模型都会生成不同的主题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15067734/
我刚刚开始阅读有关潜在狄利克雷分配 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 的主题分布。然而,正如您从下面的最后一行中看到的那样,我将其分配给了最可能的主题。 我的问题是这样的。我必须第二次运行
我是一名优秀的程序员,十分优秀!