我正在使用python gensim从231个句子的小型语料库中训练潜在Dirichlet分配(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 = …Run Code Online (Sandbox Code Playgroud) 我已经读过,最常用的主题建模技术(从文本中提取可能的主题)是Latent Dirichlet分配(LDA).
但是,我感兴趣的是,尝试使用Word2Vec进行主题建模是一个好主意,因为它会在向量空间中聚集单词.因此,不能将集群视为主题吗?
你认为为了一些研究而采用这种方法是否有意义?最后我感兴趣的是根据主题从文本中提取关键字.
我有兴趣使用Spark MLlib应用LDA主题建模.我已经检查了这里的代码和解释,但我找不到如何使用模型然后在一个新的看不见的文档中找到主题分布.
我使用Python的Gensim库训练有素的Word2vec模型.我有一个标记化列表如下.词汇大小是34,但我只是给出了34个中的一些:
b = ['let',
'know',
'buy',
'someth',
'featur',
'mashabl',
'might',
'earn',
'affili',
'commiss',
'fifti',
'year',
'ago',
'graduat',
'21yearold',
'dustin',
'hoffman',
'pull',
'asid',
'given',
'one',
'piec',
'unsolicit',
'advic',
'percent',
'buy']
Run Code Online (Sandbox Code Playgroud)
模型
model = gensim.models.Word2Vec(b,min_count=1,size=32)
print(model)
### prints: Word2Vec(vocab=34, size=32, alpha=0.025) ####
Run Code Online (Sandbox Code Playgroud)
如果我尝试通过model['buy']列表中的一个单词来获得相似性得分,我得到了
KeyError:"词'买'不在词汇中"
你们可以告诉我我做错了什么以及检查模型的方法有哪些可以进一步用于训练PCA或t-sne以便可视化形成主题的类似单词?谢谢.
我正在使用这个LDA包用于R.特别是我正在尝试进行监督潜在的dirichlet分配(slda).在链接包中,有一个slda.em功能.但令我困惑的是它要求alpha,eta和variance参数.据我了解,我认为这些参数在模型中是未知的.所以我的问题是,包的作者是否意味着这些是参数的初步猜测?如果是,似乎没有办法从运行结果中访问它们slda.em.
除了编码算法中的额外EM步骤之外,是否有建议的方法来猜测这些参数的合理值?
我使用Latent Dirichlet Allocation(sklearn实现)来分析大约500篇科学文章摘要,并且我得到了包含最重要单词的主题(用德语).我的问题是解释与最重要的单词相关的这些值.我假设每个主题的所有单词的概率加起来为1,但实际情况并非如此.
我怎样才能解释这些价值观?例如,我希望能够说明为什么主题#20的单词值比其他主题高得多.他们的绝对高度与贝叶斯概率有关吗?该主题在语料库中更常见吗?我还没有把这些价值观与LDA背后的数学结合在一起.
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.decomposition import LatentDirichletAllocation
tf_vectorizer = CountVectorizer(max_df=0.95, min_df=1, top_words=stop_ger,
analyzer='word',
tokenizer = stemmer_sklearn.stem_ger())
tf = tf_vectorizer.fit_transform(texts)
n_topics = 10
lda = LatentDirichletAllocation(n_topics=n_topics, max_iter=5,
learning_method='online',
learning_offset=50., random_state=0)
lda.fit(tf)
def print_top_words(model, feature_names, n_top_words):
for topic_id, topic in enumerate(model.components_):
print('\nTopic Nr.%d:' % int(topic_id + 1))
print(''.join([feature_names[i] + ' ' + str(round(topic[i], 2))
+' | ' for i in topic.argsort()[:-n_top_words - 1:-1]]))
n_top_words = 4
tf_feature_names = tf_vectorizer.get_feature_names()
print_top_words(lda, tf_feature_names, n_top_words)
Topic …Run Code Online (Sandbox Code Playgroud) 导入 top2vec 时我不断收到此错误。
TypeError Traceback (most recent call last)
Cell In [1], line 1
----> 1 from top2vec import Top2Vec
File ~\AppData\Roaming\Python\Python39\site-packages\top2vec\__init__.py:1
----> 1 from top2vec.Top2Vec import Top2Vec
3 __version__ = '1.0.27'
File ~\AppData\Roaming\Python\Python39\site-packages\top2vec\Top2Vec.py:12
10 from gensim.models.phrases import Phrases
11 import umap
---> 12 import hdbscan
13 from wordcloud import WordCloud
14 import matplotlib.pyplot as plt
File ~\AppData\Roaming\Python\Python39\site-packages\hdbscan\__init__.py:1
----> 1 from .hdbscan_ import HDBSCAN, hdbscan
2 from .robust_single_linkage_ import RobustSingleLinkage, robust_single_linkage
3 from .validity import validity_index
File ~\AppData\Roaming\Python\Python39\site-packages\hdbscan\hdbscan_.py:509
494 row_indices …Run Code Online (Sandbox Code Playgroud) 主题建模可识别文档集合中主题的分布,从而有效地识别集合中的集群.那么说主题建模是一种进行文档聚类的技术是正确的吗?
(我正在使用R.)对于一个名为"goodwords.corpus"的单词列表,我循环遍历语料库中的文档,并将单词"goodwords.corpus"中的每个单词替换为单词+ a数.
因此,例如,如果列表中包含"good"一词,并且列表中没有"晚安",则此文档:
I am having a good time goodnight
Run Code Online (Sandbox Code Playgroud)
会变成:
I am having a good 1234 time goodnight
Run Code Online (Sandbox Code Playgroud)
**我正在使用此代码(EDIT-使此可重现):
goodwords.corpus <- c("good")
test <- "I am having a good time goodnight"
for (i in 1:length(goodwords.corpus)){
test <-gsub(goodwords.corpus[[i]], paste(goodwords.corpus[[i]], "1234"), test)
}
Run Code Online (Sandbox Code Playgroud)
但是,问题是我希望gsub只替换整个单词.出现的问题是:"goodwords"在"goodwords.corpus"列表中,但是"goodnight"(不在列表中)也会受到影响.所以我明白了:
I am having a good 1234 time good 1234night
Run Code Online (Sandbox Code Playgroud)
无论如何,我可以告诉gsub只能替换整个单词,而不是可能是其他单词的一部分的单词吗?
我想用这个:
test <-gsub("\\<goodwords.corpus[[i]]\\>", paste(goodwords.corpus[[i]], "1234"), test)
}
Run Code Online (Sandbox Code Playgroud)
我读过\ <和\>会告诉gsub只查找整个单词.但显然这不起作用,因为当引用时,goodwords.corpus [[i]]将不起作用.
有什么建议?
我有一个数据集并尝试使用 berTopic 建模将其转换为主题,但问题是,我无法获取主题的所有文档。berTopic 每个主题仅返回 3 个文档。
topic_model = BERTopic(verbose=True, embedding_model=embedding_model,
nr_topics = 'auto',
n_gram_range = (3,3),
top_n_words = 10,
calculate_probabilities=True,
seed_topic_list = topic_list,
)
topics, probs = topic_model.fit_transform(docs_test)
representative_doc = topic_model.get_representative_docs(topic#1)
representative_doc
Run Code Online (Sandbox Code Playgroud)
该主题包含超过 300 个文档,但 bertopic 仅显示其中 3 个.get_representative_docs
topic-modeling ×10
nlp ×4
lda ×3
python ×3
gensim ×2
r ×2
word2vec ×2
apache-spark ×1
dirichlet ×1
gsub ×1
python-3.x ×1
scikit-learn ×1