Skip to content

sklearn.decomposition.PCA svd_solver='auto' should default to "arpack" for sparse input  #7642

Closed

Description

@nirizr

I'm using sklearn version 0.18

When using sklearn.decomposition.PCA on a scipy.sparse, the value provided in the svd_solver parameter must be "arpack" (which is the only algorithm supporting sparse input).

the default svd_solver value, "auto" presumes to pick an appropriate default value depending on other input parameters (such as matrix shape and n_components).

It would be helpful if "auto" will also take sparsity into consideration.

Additionally, svd_solver value "full" (or "auto" that picks "full") yields the following error:

TypeError: A sparse matrix was passed, but dense data is required. Use X.toarray() to convert to a dense numpy array.

It would be helpful if the error included a suggestion to use svd_solver value of "arpack" as a potential fix.

Activity

  1. added
    Easy
    on Oct 11, 2016
  2. amueller commented on Oct 11, 2016

    @amueller
    Member

    Actually, randomized should also work. Anything but full would be fine. So the "auto" method should be fixed (not sure if arpack or randomized is more appropriate as default) and the error message should be fixed as you indicated.

    ping @giorgiop who knows all about the benchmarks ;)

  3. nirizr commented on Oct 11, 2016

    @nirizr
    Author

    not sure if arpack or full is more appropriate as default

    Did you mean "randomized" where you've used "full" in your previous message?

  4. amueller commented on Oct 11, 2016

    @amueller
    Member

    @nirizr in one place, yes. I fixed it.

  5. nirizr commented on Oct 11, 2016

    @nirizr
    Author

    Actually, it looks like sklearn.decomposition.PCA doesn't nicely support sparse matrices as much as I'd hope.

    It ends up getting to this piece of code:

    self.mean_ = np.mean(X, axis=0)
    X -= self.mean_
    

    Which forces X to become dense.

  6. removed
    Easy
    on Oct 11, 2016
  7. amueller commented on Oct 11, 2016

    @amueller
    Member

    D'oh. You should use TruncatedSVD and with any setting PCA should tell you not to use it with sparse data. The warning should tell you to use truncatedSVD though.

  8. nirizr commented on Oct 11, 2016

    @nirizr
    Author

    Oh, I didn't see that, thanks and sorry for the trouble!

  9. added
    Easy
    on Oct 11, 2016
  10. amueller commented on Oct 11, 2016

    @amueller
    Member

    I guess we could provide a better error. PR welcome.

  11. giorgiop commented on Oct 12, 2016

    @giorgiop
    Contributor

    Yeah, PCA should state clearly that it doesn't support sparse inputs. I am going to make a PR.

  12. nirizr commented on Oct 18, 2016

    @nirizr
    Author

    I guess this can be closed now?

    Thank you @giorgiop!

  13. giorgiop commented on Oct 18, 2016

    @giorgiop
    Contributor

    Yes. Thanks for spotting this!

  14. nirizr commented on Oct 18, 2016

    @nirizr
    Author

    Alright. closing. Thanks again guys!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocumentationEasy

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions