Scikit-learn (sklearn) PCA throws Type Error on sparse matrix

我们两清 提交于 2019-12-11 03:25:38

问题


From the documentation of sklearn RandomizedPCA, sparse matrices are accepted as input. However when I called it with a sparse matrix, I got a TypeError :

> sklearn.__version__
'0.16.1'
> pca = RandomizedPCA(n_components=2)
> pca.fit(my_sparce_mat)
TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.

I obtained the same error using fit_transform.

Any suggestion on how to have it work?


回答1:


The answer is that it is not possible to have RandomizedPCA work with a sparse matrix with version 0.16.1 of Scikit-learn (current stable version). The documentation I was referring to was for a previous version of Scikit-learn and so alternative functions should be used for the current stable version.

A possible alternative is TruncatedSVD



来源:https://stackoverflow.com/questions/30242215/scikit-learn-sklearn-pca-throws-type-error-on-sparse-matrix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!