New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MRG+1] Made PCA expose the singular values #7685

Merged
merged 28 commits into from Oct 30, 2016

Conversation

Projects
None yet
3 participants
@tomlof
Contributor

tomlof commented Oct 17, 2016

Reference Issue

Fixes #6955.

What does this implement/fix? Explain your changes.

I've made it so that the singular values from the underlying SVD are stored in the PCA classes under the attribute "singular_values_".

This was a trivial fix, and all I did was to save the singular values in an instance variable. I also added the attribute to the list of attributes in the documentations and added doc tests for them. I also added unit tests (test_singular_values) that cover the estimators PCA, IncrementalPCA and TruncatedSVD to the corresponding tests in sklearn/decomposition/tests.

Any other comments?

It may appear like there are loads of commits in this fix, but the commits listed below are some old stuff I was working on years ago, that are no longer in my fork. This fix only add changes to the modules: "decomposition/incremental_pca.py", "decomposition/pca.py" and "decomposition/truncated_pca.py" and to their corresponding unit tests, and the changes are made on the latest commit to the main scikit-learn repository.

@amueller

Looks good apart from minor comments. I'm not sure whether we want a test or not.

Show outdated Hide outdated sklearn/decomposition/incremental_pca.py
@@ -166,6 +171,7 @@ def fit(self, X, y=None):
self.singular_values_ = None
self.explained_variance_ = None
self.explained_variance_ratio_ = None
self.singular_values_ = None

This comment has been minimized.

@amueller

amueller Oct 17, 2016

Member

Is there any reason to add this here?

@amueller

amueller Oct 17, 2016

Member

Is there any reason to add this here?

This comment has been minimized.

@tomlof

tomlof Oct 18, 2016

Contributor

Not really, but the other attributes were set there, so I thought it would be good for consistency to have the singular values be set there as well.

@tomlof

tomlof Oct 18, 2016

Contributor

Not really, but the other attributes were set there, so I thought it would be good for consistency to have the singular values be set there as well.

Show outdated Hide outdated sklearn/decomposition/pca.py
Show outdated Hide outdated sklearn/decomposition/pca.py
Show outdated Hide outdated sklearn/decomposition/truncated_svd.py
@@ -385,6 +396,7 @@ def _fit_full(self, X, n_components):
explained_variance_ = (S ** 2) / n_samples
total_var = explained_variance_.sum()
explained_variance_ratio_ = explained_variance_ / total_var
singular_values_ = S.copy() # Store the singular values.

This comment has been minimized.

@jnothman

jnothman Oct 19, 2016

Member

Can't this be calculated by the user as np.sqrt(explained_variance_ * n_samples)?

@jnothman

jnothman Oct 19, 2016

Member

Can't this be calculated by the user as np.sqrt(explained_variance_ * n_samples)?

This comment has been minimized.

@jnothman

jnothman Oct 19, 2016

Member

Sorry. Stupid question. I've read the issue and figure this is all about making something comparable available in TruncatedSVD.

@jnothman

jnothman Oct 19, 2016

Member

Sorry. Stupid question. I've read the issue and figure this is all about making something comparable available in TruncatedSVD.

@jnothman

This comment has been minimized.

Show comment
Hide comment
@jnothman

jnothman Oct 19, 2016

Member

Please add tests.

Member

jnothman commented Oct 19, 2016

Please add tests.

@tomlof

This comment has been minimized.

Show comment
Hide comment
@tomlof

tomlof Oct 25, 2016

Contributor

Do you mean to add more doc tests, or to add a unit test?

Contributor

tomlof commented Oct 25, 2016

Do you mean to add more doc tests, or to add a unit test?

@amueller

This comment has been minimized.

Show comment
Hide comment
@amueller

amueller Oct 25, 2016

Member

unit test

Member

amueller commented Oct 25, 2016

unit test

@tomlof

This comment has been minimized.

Show comment
Hide comment
@tomlof

tomlof Oct 25, 2016

Contributor

Ok, I've added unit tests for the singular values to PCA, IncrementalPCA and TruncatedSVD.

Contributor

tomlof commented Oct 25, 2016

Ok, I've added unit tests for the singular values to PCA, IncrementalPCA and TruncatedSVD.

@amueller

This comment has been minimized.

Show comment
Hide comment
@amueller

amueller Oct 26, 2016

Member

Thanks. The test are failing though.

Member

amueller commented Oct 26, 2016

Thanks. The test are failing though.

@jnothman

LGTM and thanks for the great tests! I only wonder if we should be refactoring the tests.

@jnothman jnothman changed the title from Made PCA expose the singular values to [MRG+1] Made PCA expose the singular values Oct 26, 2016

@amueller

This comment has been minimized.

Show comment
Hide comment
@amueller

amueller Oct 27, 2016

Member

yeah it would be nice not to duplicate the code as much, bot otherwise LGTM.

Member

amueller commented Oct 27, 2016

yeah it would be nice not to duplicate the code as much, bot otherwise LGTM.

@jnothman

This comment has been minimized.

Show comment
Hide comment
@jnothman

jnothman Oct 27, 2016

Member

@tomlof please add an entry in what's new and let us know so we can merge.

Member

jnothman commented Oct 27, 2016

@tomlof please add an entry in what's new and let us know so we can merge.

@tomlof

This comment has been minimized.

Show comment
Hide comment
@tomlof

tomlof Oct 27, 2016

Contributor

Ok. I've updated the pull request description so that it mentions the unit tests.

Contributor

tomlof commented Oct 27, 2016

Ok. I've updated the pull request description so that it mentions the unit tests.

@jnothman

This comment has been minimized.

Show comment
Hide comment
@jnothman

jnothman Oct 29, 2016

Member

What's new is doc/whats_new.rst, our changelog.

Member

jnothman commented Oct 29, 2016

What's new is doc/whats_new.rst, our changelog.

@jnothman jnothman merged commit 4ae1013 into scikit-learn:master Oct 30, 2016

3 checks passed

ci/circleci Your tests passed on CircleCI!
Details
continuous-integration/appveyor/pr AppVeyor build succeeded
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
@jnothman

This comment has been minimized.

Show comment
Hide comment
@jnothman

jnothman Oct 30, 2016

Member

Thanks @tomlof

Member

jnothman commented Oct 30, 2016

Thanks @tomlof

@tomlof tomlof deleted the tomlof:pca_expose_singular_values branch Nov 2, 2016

@amueller

This comment has been minimized.

Show comment
Hide comment
@amueller

amueller Nov 16, 2016

Member

This seems to have cause a test failure in master: #7893

Member

amueller commented Nov 16, 2016

This seems to have cause a test failure in master: #7893

sergeyf added a commit to sergeyf/scikit-learn that referenced this pull request Feb 28, 2017

afiodorov added a commit to unravelin/scikit-learn that referenced this pull request Apr 25, 2017

Sundrique added a commit to Sundrique/scikit-learn that referenced this pull request Jun 14, 2017

paulha added a commit to paulha/scikit-learn that referenced this pull request Aug 19, 2017

maskani-moh added a commit to maskani-moh/scikit-learn that referenced this pull request Nov 15, 2017

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