3

When using pyLDAvis.sklearn.prepare to visualize an LDA topic model, I encountered the following error message:

>>> pyLDAvis.sklearn.prepare(lda_model, dtm, vectorizer)
...
---> 12     return dtm.sum(axis=1).getA1()
...
AttributeError: 'numpy.ndarray' object has no attribute 'getA1'

Passing dtm into pyLDAvis.sklearn.prepare as a pd.DataFrame raises a similar error:

AttributeError: 'Series' object has no attribute 'getA1'

Why is this error message occurring?

Miles Erickson
  • 2,574
  • 2
  • 17
  • 18

1 Answers1

5

The missing getA1 method exists only for numpy.matrix objects. There is no numpy.ndarray.getA1 method, nor is there a pandas.Series.getA1 method.

Casting the document vectors to a numpy.matrix resolves the error:

import pyLDAvis
import pyLDAvis.sklearn
pyLDAvis.enable_notebook()

dtm = np.matrix(document_vectors_arr)
pyLDAvis.sklearn.prepare(lda_model, dtm, vectorizer)
Miles Erickson
  • 2,574
  • 2
  • 17
  • 18