25

I can't import pyLDAvis.

It is installed but for some reason, I can not import it.

I tried

conda update anaconda

pip install --upgrade pip

pip install --upgrade jupyter notebook

pip install pyLDAvis

Installing pyLDAvis returns the message 'requirement already satisfied'. So I tried uninstalling and reinstalled the package but still doesn't work. This never happened with any other packages.

How can I solve this problem?

Dohun
  • 477
  • 2
  • 7
  • 13
  • 2
    Does this answer your question? [ModuleNotFoundError: No module named 'pyLDAvis' in anaconda spyder](https://stackoverflow.com/questions/50946003/modulenotfounderror-no-module-named-pyldavis-in-anaconda-spyder) – NStavrakoudis Mar 23 '21 at 08:49

8 Answers8

64

The pyLDAvis gensim name changed. When I use gensim_models rather than gensim the interactive viz works.

The 'gensim_models' name is in the latest commit to bmabey's repo.

import pyLDAvis
import pyLDAvis.gensim_models as gensimvis
pyLDAvis.enable_notebook()

# feed the LDA model into the pyLDAvis instance
lda_viz = gensimvis.prepare(ldamodel, corpus, dictionary)
script_kitty
  • 761
  • 4
  • 8
15

Following code worked for me and I'm using Google Colaboratory.

!pip install pyLDAvis

import pyLDAvis
import pyLDAvis.gensim_models

pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim_models.prepare(ldamodel, doc_term_matrix, dictionary)
vis
toshi456
  • 213
  • 1
  • 7
4

If you are working in jupyter notebook (python vs3.3.0)

"the No module named ‘pyLDAvis.gensim’"

error can be solved using:

import pyLDAvis.gensim_models

instead of:

import pyLDAvis.gensim
Peter Csala
  • 17,736
  • 16
  • 35
  • 75
Gjuri
  • 61
  • 2
2

Try this

!pip install pyLDAvis
import pyLDAvis.gensim_models

This should work. I faced the same issue and it worked for me

NS26
  • 21
  • 2
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 03 '22 at 07:19
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 03 '22 at 11:52
1

Please follow the below

import pyLDAvis.gensim_models as gensimvis
pyLDAvis.enable_notebook()
vis = gensimvis.prepare(lda_model, corpus, dictionary)
vis
1

The pip installation may not agree with Anaconda. It is better to use conda installation.

conda install -c conda-forge pyldavis

Then it should work fine with Anaconda Python.

Mathemilda
  • 123
  • 9
0

Update your pyLDAvis package to the latest version, which includes the pyLDAvis.gensim_models module. You can do this by running the command: pip install --upgrade pyLDAvis in your terminal.

jrreda
  • 763
  • 7
  • 7
0

The updated solution is:

import pyLDAvis
from gensim.models import LdaModel

pyLDAvis.enable_notebook()
vis = pyLDAvis.gensim.prepare(ldamodel, doc_term_matrix, dictionary)
pyLDAvis.display(vis)