Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: 'numpy.ndarray' object has no attribute 'getA1'

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?

like image 884
Miles Erickson Avatar asked Dec 08 '25 01:12

Miles Erickson


1 Answers

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)
like image 134
Miles Erickson Avatar answered Dec 10 '25 13:12

Miles Erickson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!