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

From the sklearn RandomizedPCA documentation , sparse matrices are accepted as input. However, when I called it 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 got the same error using fit_transform .

Any suggestion on how to make it work?

+2
source share
1 answer

The answer lies in the fact that RandomizedPCA unable to work with a sparse matrix with version 0.16.1 Scikit-learn (current stable version). The documentation I referred to was for a previous version of Scikit-learn, so alternative functions should be used for the current stable version.

A possible alternative is TruncatedSVD

+2
source

All Articles