我希望使用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]]