AttributeError:“numpy.ndarray”对象没有属性“getA1”

Mil*_*son 3 python numpy pandas pyldavis

在使用pyLDAvis.sklearn.prepare可视化LDA主题模型时,遇到以下错误消息:

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

dtm作为pyLDAvis.sklearn.preparea传递pd.DataFrame会引发类似的错误:

AttributeError: 'Series' object has no attribute 'getA1'
Run Code Online (Sandbox Code Playgroud)

为什么会出现此错误消息?

Mil*_*son 5

缺少的getA1方法仅适用于numpy.matrix对象。没有numpy.ndarray.getA1办法,也没有办法pandas.Series.getA1

将文档向量转换为 anumpy.matrix可解决错误:

import pyLDAvis
import pyLDAvis.sklearn
pyLDAvis.enable_notebook()

dtm = np.matrix(document_vectors_arr)
pyLDAvis.sklearn.prepare(lda_model, dtm, vectorizer)
Run Code Online (Sandbox Code Playgroud)