5

I am trying to visually depict my topics in python using pyldavis. However i am unable to view the graph. Is it that we have to view the graph in the browser or will it get popped upon execution. Below is my code

import pyLDAvis
import pyLDAvis.gensim as gensimvis
print('Pyldavis ....')
vis_data = gensimvis.prepare(ldamodel, doc_term_matrix, dictionary)
pyLDAvis.display(vis_data)

The program is continuously in execution mode on executing the above commands. Where should I view my graph? Or where it will be stored? Is it integrated only with the Ipython notebook?Kindly guide me through this. P.S My python version is 3.5.

Deepa Huddar
  • 321
  • 1
  • 4
  • 15

4 Answers4

4

This not work:

pyLDAvis.display(vis_data)

This will work for you:

pyLDAvis.show(vis_data)

ZF007
  • 3,708
  • 8
  • 29
  • 48
cxy
  • 156
  • 1
  • 7
2

I'm facing the same problem now. EDIT: My script looks as follows:

first part:

import pyLDAvis
import pyLDAvis.sklearn
print('start script')
tf_vectorizer = CountVectorizer(strip_accents = 'unicode',stop_words = 'english',lowercase = True,token_pattern = r'\b[a-zA-Z]{3,}\b',max_df = 0.5,min_df = 10)
dtm_tf = tf_vectorizer.fit_transform(docs_raw)
lda_tf = LatentDirichletAllocation(n_topics=20, learning_method='online')
print('fit')
lda_tf.fit(dtm_tf)

second part:

print('prepare')
vis_data = pyLDAvis.sklearn.prepare(lda_tf, dtm_tf, tf_vectorizer)
print('display')
pyLDAvis.display(vis_data)

The problem is in the line "vis_data = (...)".if I run the script, it will print 'prepare' and keep on running after that without printing anything else (so it never reaches the line "print('display')).

Funny thing is, when I just run the whole script it gets stuck on that line, but when I run the first part, got to my console and execute purely the single line "vis_data = pyLDAvis.sklearn.prepare(lda_tf, dtm_tf, tf_vectorizer)" this is executed in a couple of seconds.

As for the graph, I saved it as html ("simple") and use the html file to view the graph.

Keess
  • 69
  • 7
0

I ran into the same problem (I use PyCharm as IDE) The problem is that pyLDAvize is developed for Ipython (see the docs, https://media.readthedocs.org/pdf/pyldavis/latest/pyldavis.pdf, page 3). My fix/workaround:

  • make a dict of lda_tf, dtm_tf, tf_vectorizer (eg., pyLDAviz_dict)dump the dict to a file (eg mydata_pyLDAviz.pkl)
  • read the pkl file into notebook (I did get some depreciation info from pyLDAviz, but that had no effect on the end result)
  • play around with pyLDAviz in notebook
  • if you're happy with the view, dump it into html

The cause is (most likely) that pyLDAviz expects continuous user interaction (including user-initiated "exit"). However, I rather dump data from a smart IDE and read that into jupyter, than develop/code in jupyter notebook. That's pretty much like going back to before-emacs times.

From experience this approach works quite nicely for other plotting rountines

user9165100
  • 371
  • 3
  • 11
-1

If you received the module error pyLDA.gensim, then try this one instead:

import pyLdAvis.gensim_models

You get the error because of a new version update.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77