Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[MRG+1] Added _fit_svd_solver variable to PCA #11225

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

Merged
merged 4 commits into from
Jun 13, 2018
Merged

[MRG+1] Added _fit_svd_solver variable to PCA #11225

merged 4 commits into from
Jun 13, 2018

Conversation

njkevlani
Copy link
Contributor

Fixes #11223 Expose solver used with PCA(svd_solver='auto')

_fit_method will be set as soon as fit(X) method is called. Depending upon the value passes for svd_solver and input array X, value of _fit_method will be set.

_fit_method will be set as soon as fit(X) method is called. Depending upon the value passes for svd_solver and input array X, value of _fit_method will be set.
@@ -388,7 +388,7 @@ def _fit(self, X):
else:
n_components = self.n_components

# Handle svd_solver
# Handle svd_solver and _fit_method
svd_solver = self.svd_solver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can replace variable svd_solver with the new self._fit_method

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everywhere? IMO svd_solver is more concise/readable..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

svd_solver is just being a local variable in _fit() method. If we replace it with a class variable (say self._fit_svd_solver for now), it will serve both the purposes, doing svd_solver's current task and expose the solver used.

Moreover, NearestNeighbours also handle algorithm='auto' in this manner.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but you would have to do 4-5 replacements of svd_solver with self._fit_svd_solver to save one assignment, self._fit_svd_solver = svd_solver at the end (current solution). In any case that's not too important, either way would be fine, up to you.

@qinhanmin2014
Copy link
Member

Generally we need tests for a PR related to source code, but since _fit_method is private and we also don't have related test in sklearn.neighbor, I'm fine not to add a test here.

@jnothman
Copy link
Member

jnothman commented Jun 10, 2018 via email

@njkevlani
Copy link
Contributor Author

@jnothman Will it be okay if we overwrite value of self.svd_solver if it is passed as "auto"? By doing this we will solve the issue and maintain less variables.

@rth
Copy link
Member

rth commented Jun 10, 2018

Will it be okay if we overwrite value of self.svd_solver if it is passed as "auto"?

No that wont work, because, when running,

pca = PCA(svd_solver='auto')
pca.fit(X1)
pca.fit(X2)

you wouldn't want the solver in the second fit to depend on the choice in the first one.

I'm not sure that _fit_method is a great name... I'd rather have "solver in
there"

+1 maybe _fit_svd_solver ? Not great, but I can't think of something better.

Generally we need tests for a PR related to source code, but since _fit_method is private and we also don't have related test in sklearn.neighbor, I'm fine not to add a test here.

Yes, though a test for two repeated fits (see above) behave consistently for the solver wouldn't hurt. Unless there is already a test like that.

@njkevlani
Copy link
Contributor Author

@rth

No that wont work, because, when running,

pca = PCA(svd_solver='auto')
pca.fit(X1)
pca.fit(X2)

Agree with that. How about this?

@qinhanmin2014
Copy link
Member

maybe _fit_svd_solver ?

I'm OK with it and actually don't care much about the name (since it's private). Maybe we can also consider self._svd_solver? So we have self.svd_solver for parameter svd_solver and self._svd_solver for the solver used by PCA.

Replacing svd_solver with self._fit_svd_solver let us keep track of svd_solver method even if it is passed as 'auto'
Copy link
Member

@qinhanmin2014 qinhanmin2014 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @njkevlani

@@ -388,26 +388,26 @@ def _fit(self, X):
else:
n_components = self.n_components

# Handle svd_solver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a serious problem but I think you might leave the comment as it is. (Handle _fit_svd_solver seems awkward from my side.)

@qinhanmin2014 qinhanmin2014 changed the title Added _fit_method variable [MRG+1] Added _fit_svd_solver variable to PCA Jun 11, 2018
# Handle svd_solver
svd_solver = self.svd_solver
if svd_solver == 'auto':
# Handle _svd_solver
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_svd_solver -> svd_solver :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@qinhanmin2014 Fixed typo.

@njkevlani
Copy link
Contributor Author

@qinhanmin2014 @rth Would you guys want me to change anything further?

Copy link
Member

@rth rth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, merging. Thank you @njkevlani !

(I would have preferred to the keep the local svd_solver variable as less verbose, but since there is no consensus on that, this is fine.)

@rth rth merged commit bb38539 into scikit-learn:master Jun 13, 2018
@njkevlani njkevlani deleted the fit_method-pca branch June 13, 2018 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants