michael
michael

Reputation: 391

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

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?

Upvotes: 2

Views: 869

Answers (1)

michael
michael

Reputation: 391

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

Upvotes: 2

Related Questions