我希望使用LDA将每个文档分配给一个主题。现在我意识到你得到的是来自LDA的主题的分布。但是,正如您从下面最后一行中看到的,我将其指定为最可能的主题。
我的问题是这个。为了获得这些主题,我不得不第二次运行lda[corpus]。还有其他内置的gensim函数可以直接给我这个主题赋值向量吗?特别是由于LDA算法已经通过文档,它可能已经保存了这些主题分配?
# Get the Dictionary and BoW of the corpus after some stemming/ cleansing
texts = [[stem(word) for word in document.split() if word not in STOPWORDS] for document in cleanDF.text.values]
dictionary = corpora.Dictionary(texts)
dictionary.filter_extremes(no_below=5, no_above=0.9)
corpus = [dictionary.doc2bow(text) for text in texts]
# The actual LDA component
lda = models.LdaMulticore(corpus=corpus, id2word=dictionary, num_topics=30, chunksize=10000, passes=10,workers=4)
# Assign each document to most prevalent topic
lda_topic_assignment = [max(p,key=lambda item: item[1]) for p in lda[corpus]]发布于 2019-01-19 15:53:31
没有其他内置的Gensim函数可以直接给出主题赋值向量。
您的问题是,LDA算法已经通过文档传递,但是LDA的实现是通过块更新模型(基于chunksize参数的值)来实现的,因此它不会将整个语料库保存在内存中。
因此,您必须使用lda[corpus]或lda.get_document_topics()方法。
发布于 2018-02-20 13:25:28
dictionary = corpora.Dictionary(texts)
corpus = [dictionary.doc2bow(text) for text in texts]
test =LDA[corpus[0]]
print(test)
sorted(test, reverse=True, key=lambda x: x[1])
Topics = ['Topic_'+str(sorted(LDA[i], reverse=True, key=lambda x: x[1])[0][0]).zfill(3) for i in corpus]https://stackoverflow.com/questions/39969919
复制相似问题