Skip to content

Store singular values in PCA instances #6955

Closed

Description

@dchandan

Is there a particular reason why classes such as PCA, TruncatedPCA do not store the singular values as instance variables after they have been computed? It would seem useful to store them since they are already computed so that they can be later accessed if need be. One use case would be to scale the right singular vectors by their singular values. I am happy to make the changes and push if there are no objections.

Activity

  1. lesteve commented on Jul 4, 2016

    @lesteve
    Member

    From a quick look at it, it looks like you can compute them from the explained_variance_ attribute.

    n_samples = X.shape[0]
    self.explained_variance_ = (S ** 2) / n_samples

    S being the diagonal matrix with the singular values.

  2. dchandan commented on Jul 4, 2016

    @dchandan
    Author

    True, but a few comments:

    • It should be possible to access the singular values directly as a class attribute (especially since it is computed anyways) rather than indirectly
    • By PCA instances i meant to include PCA, TruncatedPCA and others. While indirectly computing the singular values from PCA is straightforward using explained_variance_, it is not possible (to my reading) from TruncatedPCA whose calculation for the explained_variance_ is more convoluted.
    • It seems like IncrementalPCA already exposes a class attribute self.singular_values_, so it would make sense to expose a similar attribute in other PCA related classes as well.
  3. amueller commented on Oct 13, 2016

    @amueller
    Member

    I guess there is no harm in storing S.

  4. added
    Easy
    on Oct 13, 2016
  5. tomlof commented on Oct 17, 2016

    @tomlof
    Contributor

    I needed this in my work, so I fixed this issue and have sent a pull request. I added it to PCA, RandomizedPCA and TruncatedSVD.

    It could also be added to SparsePCA, but the singular values are not computed in the fit, but only in the transform, so I left it out of my fix. They could be added to the fit, but that would increase the computational load of the fit method.

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

    EasyEnhancement

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions