python - pyLDAvis : Validation error on trying to visualize topics

标签 python nlp lda topic-modeling

我尝试使用 gensim 为 300000 条记录生成主题。在尝试可视化主题时,我收到验证错误。我可以在模型训练后打印主题,但在使用 pyLDAvis 时失败

# Running and Training LDA model on the document term matrix.
ldamodel1 = Lda(doc_term_matrix1, num_topics=10, id2word = dictionary1, passes=50, workers = 4)

(ldamodel1.print_topics(num_topics=10, num_words = 10))
 #pyLDAvis
d = gensim.corpora.Dictionary.load('dictionary1.dict')
c = gensim.corpora.MmCorpus('corpus.mm')
lda = gensim.models.LdaModel.load('topic.model')

#error on executing this line
data = pyLDAvis.gensim.prepare(lda, c, d)

在 pyLDAvis 上运行后,我在尝试时遇到以下错误

---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
<ipython-input-53-33fd88b65056> in <module>()
----> 1 data = pyLDAvis.gensim.prepare(lda, c, d)
      2 data

C:\ProgramData\Anaconda3\lib\site-packages\pyLDAvis\gensim.py in prepare(topic_model, corpus, dictionary, doc_topic_dist, **kwargs)
    110     """
    111     opts = fp.merge(_extract_data(topic_model, corpus, dictionary, doc_topic_dist), kwargs)
--> 112     return vis_prepare(**opts)

C:\ProgramData\Anaconda3\lib\site-packages\pyLDAvis\_prepare.py in prepare(topic_term_dists, doc_topic_dists, doc_lengths, vocab, term_frequency, R, lambda_step, mds, n_jobs, plot_opts, sort_topics)
    372    doc_lengths      = _series_with_name(doc_lengths, 'doc_length')
    373    vocab            = _series_with_name(vocab, 'vocab')
--> 374    _input_validate(topic_term_dists, doc_topic_dists, doc_lengths, vocab, term_frequency)
    375    R = min(R, len(vocab))
    376 

C:\ProgramData\Anaconda3\lib\site-packages\pyLDAvis\_prepare.py in _input_validate(*args)
     63    res = _input_check(*args)
     64    if res:
---> 65       raise ValidationError('\n' + '\n'.join([' * ' + s for s in res]))
     66 
     67 

ValidationError: 
 * Not all rows (distributions) in topic_term_dists sum to 1.

最佳答案

发生这种情况是因为 pyLDAvis 程序期望模型中的所有文档主题至少在语料库中出现一次。当您在制作语料库/文本之后和制作模型之前进行一些预处理时,可能会发生这种情况。

模型内部词典中的某个词未在您提供的词典中使用,这将导致失败,因为现在概率略小于 1。

您可以通过将缺失的词添加到您的语料库词典(或将词添加到语料库并从中制作词典)来解决此问题,或者您可以将此行添加到 site-packages\pyLDAvis\gensim.py 代码在“断言 topic_term_dists.shape[0] == doc_topic_dists.shape[1]”之前(应该是~第 67 行)

topic_term_dists = topic_term_dists / topic_term_dists.sum(axis=1)[:, None]

假设您的代码运行到那个点,这应该重新规范化主题分布而不丢失字典项。但请注意,最好将所有术语都包含在语料库中。

关于python - pyLDAvis : Validation error on trying to visualize topics,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47998685/

相关文章:

machine-learning - 为什么我的神经网络序列模型从一开始就达到 0.9998 的精度?

regex - 使用 Regex 删除括号和其中的所有内容

从 R 主题模型中的 DocumentTermMatrix 中删除空文档?

scala - 在spark中为LDA准备数据

python - 通过 libPd 从 Python 向 Pure Data 补丁发送消息

python - 如何以编程方式从 Python 提升到 Visual Studio 的顶部

python - awscli 工具由 pip 安装并在 pip 中显示,但在访问时 - 找不到命令

python - 如何使用以任何大写字母开头的正则表达式从 Pandas 系列中提取字符串

python - 如何向量化一系列标记

machine-learning - 使用 scikit 的 LatentDirichletAllocation 类训练时评估模型