0

I am trying to visualise results of an LDA Model using PyLDAvis. I have managed to get the graphs to display in jupyter notebook, however, the labels of the keywords describing the topics (on the bar chart) are missing.

Below is an example of the code using dummy data.

import numpy as np
import pandas as pd
import gensim
import gensim.corpora as corpora
import matplotlib.pyplot as plt
import pyLDAvis
import pyLDAvis.gensim 

texts = [['today', 'cold', 'winter', 'lot', 'snow', 'everywhere'],
       ['winter', 'snow', 'snowman'],
       ['summer', 'day', 'sunny', 'lake'],
       ['sunny', 'garden', 'today'],
       ['winter', 'snowing', 'today'],
       ['picnic', 'garden', 'sunny', 'weekend']]

# Create Dictionary
dictionary = corpora.Dictionary(texts)

# Create Corpus
corpus = [dictionary.doc2bow(text) for text in texts]

# Train model
lda_model = gensim.models.ldamodel.LdaModel(corpus=corpus,
                                           id2word=dictionary,
                                           num_topics=3, 
                                           random_state=100,
                                           update_every=1,
                                           chunksize=100,
                                           passes=10,
                                           alpha='auto',
                                           per_word_topics=True)

# Visualise topics
pyLDAvis.enable_notebook()
plot = pyLDAvis.gensim.prepare(lda_model, corpus, dictionary)
plot

Here is a screenshot of the plot that is displayed

Here is an example of what it should look like https://nbviewer.jupyter.org/github/bmabey/hacker_news_topic_modelling/blob/master/HN%20Topic%20Model%20Talk.ipynb

I have tried setting local parameter to True but it made no difference. pyLDAvis.enable_notebook(local = True)

I tried saving the output as an html but it produced an empty file. pyLDAvis.save_html(plot, 'lda.html')

I'm using python 3.7.1

Any suggestions?

2 Answers2

1

!pip install pyLDAvis==2.1.2

I got this problem as well and this helped. Older version of pyLDAvis does not work properly with Jupyter or Colab.

Ruli
  • 2,592
  • 12
  • 30
  • 40
  • Thanks very much! Version 2.1.2 is working fine for me with Jupyter. – user15177517 Feb 14 '21 at 20:53
  • @user15177517 the answer was submited by reisen Inaba, I have only edited it. But if it works for you, instead of commenting please use the accept button to the left of the question. – Ruli Feb 14 '21 at 20:57
0

I met the same problem and I just use this:

pyLDAvis.save_html(plot, 'vis.html')

so the complete visuliazation would be saved in wd as a html file.