python中GSDMM的一个实际示例?
我想使用GSDMM为我的数据集中的一些tweet分配主题。我发现的唯一例子(和)不够详细。我想知道您是否知道一个源代码(或者是否足够关心制作一个小示例),它展示了如何使用python实现GSDMM GSDMM(Gibbs采样Dirichlet多项式混合物)是一篇短文 聚类模型。它本质上是一种改进的LDA(潜在滴漏) 分配)假设一个文档(如tweet或任何其他 文本包含一个主题 地址:python中GSDMM的一个实际示例?,python,lda,topic-modeling,tweets,Python,Lda,Topic Modeling,Tweets,我想使用GSDMM为我的数据集中的一些tweet分配主题。我发现的唯一例子(和)不够详细。我想知道您是否知道一个源代码(或者是否足够关心制作一个小示例),它展示了如何使用python实现GSDMM GSDMM(Gibbs采样Dirichlet多项式混合物)是一篇短文 聚类模型。它本质上是一种改进的LDA(潜在滴漏) 分配)假设一个文档(如tweet或任何其他 文本包含一个主题 地址: 我也在试验GSDMM,但遇到了同样的问题,就是网上的东西不多(当然,除了一些使用它的论文之外,我找不到比你
我也在试验GSDMM,但遇到了同样的问题,就是网上的东西不多(当然,除了一些使用它的论文之外,我找不到比你更多的东西)。如果你看一下GSDMGithub回购协议的代码,你会发现它是一个非常小的回购协议,只有一些功能。这些基本上都在towarddatascience的教程中使用,所以我不认为您遗漏了什么 如果您有具体问题,请随时提问
编辑:如果你遵循towardsdatascience的教程,你会意识到,这是一个不一致且尚未完成的项目。缺少某些辅助函数,并且未正确使用算法。作者使用
K=10运行它,最后得到10个集群。如果您增加K
(您应该这样做),那么集群的数量将超过10个,因此有一点作弊的情况发生。我最终为GSDMM编译了我的代码,并将从头开始将其放在这里供其他人使用。希望这有帮助。我试图对以下重要部分进行评论:
#turning sentences into words
data_words =[]
for doc in data:
doc = doc.split()
data_words.append(doc)
#building bi-grams
bigram = gensim.models.Phrases(vocabulary, min_count=5, threshold=100)
bigram_mod = gensim.models.phrases.Phraser(bigram)
print('done!')
# Removing stop Words
stop_words.extend(['from', 'rt'])
def remove_stopwords(texts):
return [[word for word in simple_preprocess(str(doc)) if word not in stop_words] for doc in texts]
data_words_nostops = remove_stopwords(vocabulary)
# Form Bigrams
data_words_bigrams = [bigram_mod[doc] for doc in data_words_nostops]
#lemmatization
data_lemmatized = []
for sent in data_words_bigrams:
doc = nlp(" ".join(sent))
data_lemmatized.append([token.lemma_ for token in doc if token.pos_ in ['NOUN', 'ADJ', 'VERB', 'ADV']])
docs = data_lemmatized
vocab = set(x for doc in docs for x in doc)
# Train a new model
import random
random.seed(1000)
# Init of the Gibbs Sampling Dirichlet Mixture Model algorithm
mgp = MovieGroupProcess(K=10, alpha=0.1, beta=0.1, n_iters=30)
vocab = set(x for doc in docs for x in doc)
n_terms = len(vocab)
n_docs = len(docs)
# Fit the model on the data given the chosen seeds
y = mgp.fit(docs, n_terms)
def top_words(cluster_word_distribution, top_cluster, values):
for cluster in top_cluster:
sort_dicts =sorted(mgp.cluster_word_distribution[cluster].items(), key=lambda k: k[1], reverse=True)[:values]
print('Cluster %s : %s'%(cluster,sort_dicts))
print(' — — — — — — — — — ')
doc_count = np.array(mgp.cluster_doc_count)
print('Number of documents per topic :', doc_count)
print('*'*20)
# Topics sorted by the number of document they are allocated to
top_index = doc_count.argsort()[-10:][::-1]
print('Most important clusters (by number of docs inside):', top_index)
print('*'*20)
# Show the top 10 words in term frequency for each cluster
top_words(mgp.cluster_word_distribution, top_index, 10)
希望这有帮助 据我所知,您拥有代码,但您需要决定如何应用它
它是如何工作的?
您可以下载论文,它表明集群搜索相当于选择桌子的游戏。想象有一群学生,并希望根据他们对电影的兴趣将他们分组。每个学生(=项目)在每一轮中切换到一个表(=集群),该表中的学生都有类似的电影,并且很受欢迎Alpha控制一个因素,该因素决定表为空时删除表的容易程度(低Alpha=更少的表)。小betas意味着选择一个表是基于与表的相似性,而不是基于表的受欢迎程度。对于短文本聚类,您采用文字而不是电影
α、β、迭代次数
因此,低alpha会导致多个包含单个单词的簇,而高alpha会导致包含更多单词的簇较少。高beta会产生受欢迎的集群,而低beta会产生类似的集群(没有强填充)。您需要的参数基于数据集。集群的数量主要由beta控制,但alpha也有(如上所述)影响。迭代次数在20次迭代后似乎是稳定的,但10次也可以
数据预处理过程
在训练算法之前,您需要创建一个干净的数据集。为此,您可以将每个文本转换为小写,删除非ASCII字符,然后应用或。在新样本上执行此过程时,您还需要应用此过程。您只需要代码链接吗?总比什么都不需要好。但至少对这个过程做一个简单的解释是理想的。谢谢,但我更多的是寻找一些实际的例子,比如你通常在中等或中等数据科学上看到的。有这么多的LDA,但很少为GSDMMI给你发送了一个适当的链接,如果他们没有足够的让我知道。谢谢,这一点。我需要一个关于如何应用GSDMM来使用python为短文本分配主题的教程。例如,必须编写什么代码以及如何使用GSDMM包(如何调整alpha和beta值等)来获得最终答案。我理解您的要求,正如我所提到的,LDA是GSDMM之母,所以让我们从LDA的许多样本开始:你可以看到阿尔法和贝塔将如何在那里调整,等等。我在下面添加了适用于我的代码。现在,我正在寻找一种方法来找到属于一个簇的每个文本的概率,以及一种数学方法来找到最佳簇数(如一致性分数)。你能想到什么吗?Hi@Pie ton,我使用的GSDMM python实现(我想你也使用它)有一个内置函数mgp.score
,在这里你可以看到,算法在为集群分配输入文本时有多确定。我使用算法对所有输入文档的确定程度的平均值来比较不同的超参数。这是一个我自己设计的metrik,因为我和你一样有同样的问题:)
#turning sentences into words
data_words =[]
for doc in data:
doc = doc.split()
data_words.append(doc)
#building bi-grams
bigram = gensim.models.Phrases(vocabulary, min_count=5, threshold=100)
bigram_mod = gensim.models.phrases.Phraser(bigram)
print('done!')
# Removing stop Words
stop_words.extend(['from', 'rt'])
def remove_stopwords(texts):
return [[word for word in simple_preprocess(str(doc)) if word not in stop_words] for doc in texts]
data_words_nostops = remove_stopwords(vocabulary)
# Form Bigrams
data_words_bigrams = [bigram_mod[doc] for doc in data_words_nostops]
#lemmatization
data_lemmatized = []
for sent in data_words_bigrams:
doc = nlp(" ".join(sent))
data_lemmatized.append([token.lemma_ for token in doc if token.pos_ in ['NOUN', 'ADJ', 'VERB', 'ADV']])
docs = data_lemmatized
vocab = set(x for doc in docs for x in doc)
# Train a new model
import random
random.seed(1000)
# Init of the Gibbs Sampling Dirichlet Mixture Model algorithm
mgp = MovieGroupProcess(K=10, alpha=0.1, beta=0.1, n_iters=30)
vocab = set(x for doc in docs for x in doc)
n_terms = len(vocab)
n_docs = len(docs)
# Fit the model on the data given the chosen seeds
y = mgp.fit(docs, n_terms)
def top_words(cluster_word_distribution, top_cluster, values):
for cluster in top_cluster:
sort_dicts =sorted(mgp.cluster_word_distribution[cluster].items(), key=lambda k: k[1], reverse=True)[:values]
print('Cluster %s : %s'%(cluster,sort_dicts))
print(' — — — — — — — — — ')
doc_count = np.array(mgp.cluster_doc_count)
print('Number of documents per topic :', doc_count)
print('*'*20)
# Topics sorted by the number of document they are allocated to
top_index = doc_count.argsort()[-10:][::-1]
print('Most important clusters (by number of docs inside):', top_index)
print('*'*20)
# Show the top 10 words in term frequency for each cluster
top_words(mgp.cluster_word_distribution, top_index, 10)