jupyter notebook Use pyLDAvis Error: AttributeError: module ‘pyLDAvis‘ has no attribute ‘gensim

Preparation
First, make sure you have the pyldavis and gensim libraries installed.

pip install gensim
pip install pyldavis

Code change
Secondly, if the error not attribute is because the version of pyldavis is different, the new version needs to be changed as follows: change all pyLDAvis.gensim to gensimvis。

import pyLDAvis.gensim as gensimvis
pyLDAvis.enable_notebook()
vis_sub_10 = gensimvis.prepare(lda_fst, corpus, dic, sort_topics = False)
vis_sub_20 = gensimvis.prepare(lda_snd, corpus, dic, sort_topics = False)
pyLDAvis.display(vis_sub_10)

Success! The effect is shown in the following figure.

Or you can install a lower version of pyldavis

pip install pyLDAvis==2.1.2

Codes:

import pyLDAvis.gensim
pyLDAvis.enable_notebook()
vis_sub_10 = pyLDAvis.gensimda_fst, corpus, dic, sort_topics = False)
vis_sub_20 = pyLDAvis.gensim.prepare(lda_snd, corpus, dic, sort_topics = False)
pyLDAvis.display(vis_sub_10)

Read More: