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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With