【发布时间】:2021-01-25 23:09:05
【问题描述】:
我想看看如何从 gensim lda 主题模型中访问字典。这在您训练 lda 模型、保存并稍后加载时尤其重要。换句话说,假设 lda_model 是在文档集合上训练的模型。要获得文档主题矩阵,可以执行以下操作或类似https://www.kdnuggets.com/2019/09/overview-topics-extraction-python-latent-dirichlet-allocation.html 中解释的操作:
def regTokenize(text):
# tokenize the text into words
import re
WORD = re.compile(r'\w+')
words = WORD.findall(text)
return words
from gensim.corpora.dictionary import Dictionary
ttext = [regTokenize(d) for d in text]
dic = Dictionary(ttext)
ttext = [dic.doc2bow(text) for text in ttext]
ttext = lda_model.get_document_topics(ttext)
但是,训练有素的 lda_model 中的字典可能与新数据不同,并在最后一行出现错误,例如:
"IndexError: index 41021 is out of bounds for axis 1 with size 41021"
有没有办法(或参数)从训练有素的lda_model 获取字典,用它代替dic = Dictionary(ttext)?非常感谢您的帮助和回答!
【问题讨论】:
标签: python dictionary gensim lda topic-modeling