Skip to content

[MRG+2] default gamma='auto' in SVC #10331

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 44 commits into from
Mar 10, 2018
Merged

Conversation

gxyd
Copy link
Contributor

@gxyd gxyd commented Dec 16, 2017

Reference Issues/PRs

Fixes #8361
Fixes #8535

What does this implement/fix? Explain your changes.

Deprecates the default SVC gamma parameter value of "auto", which is calculated as 1 / n_features, and introduces "scale", which is calculated as 1 / (n_features * X.std()).

Any other comments?

I couldn't fix all the doctests since I'm not sure how to run doctests using pytest (I asked on gitter, though haven't received a response), otherwise using make test will more time to see which docs need to be fixed.

neokt and others added 3 commits March 4, 2017 17:59
Conflicts:
	sklearn/model_selection/_search.py
	sklearn/model_selection/tests/test_search.py
@gxyd
Copy link
Contributor Author

gxyd commented Dec 16, 2017

Currently it raises an error:

E           AssertionError: Estimator SVC should not change or mutate  the parameter gamma
             from auto_deprecated to auto during fit.

@gxyd
Copy link
Contributor Author

gxyd commented Dec 16, 2017

I can fix that issue I mention above (that is not a problem). I'v one query, that should we use gamma='auto' or gamma='scale' in docs? Also a lot of examples in documentation don't actually set random_state (one of the points Raghav raised here #8535 (comment)), do I need to set random_state? Atleast for the example I touch in the documentation.

@gxyd
Copy link
Contributor Author

gxyd commented Dec 18, 2017

@amueller can you give a look over the PR, though I've few questions as well that I've mentioned.

@gxyd
Copy link
Contributor Author

gxyd commented Dec 18, 2017

Locally tests pass. I am guessing that travis uses scipy version 0.13.3, which doesn't have the power method for lil_matrix, hence the current error on travis. Travis is based on Ubuntu 14.04 (which has scipy 0.13.3 upstream) and that is causing the problem.

if self.gamma == 'auto':
if self.gamma == 'scale':
if isinstance(X, sp.spmatrix):
X_std = np.sqrt(X.power(2).mean() - (X.mean())**2)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've been able to figure out how to do this without the use of X.power.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I could simply use X.toarray()**2, but I don't think that is efficient.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe X.multiply(X)?

Copy link
Member

Choose a reason for hiding this comment

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

Why can there be a lil matrix here? We called check_X_y above with accept_sparse='csr'.

Copy link
Member

@amueller amueller left a comment

Choose a reason for hiding this comment

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

Can we maybe not deprecate auto and just change the default? not sure, though....

if self.gamma == 'auto':
if self.gamma == 'scale':
if isinstance(X, sp.spmatrix):
X_std = np.sqrt(X.power(2).mean() - (X.mean())**2)
Copy link
Member

Choose a reason for hiding this comment

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

Why can there be a lil matrix here? We called check_X_y above with accept_sparse='csr'.

@gxyd
Copy link
Contributor Author

gxyd commented Dec 18, 2017

@amueller

Can we maybe not deprecate auto and just change the default? not sure, though....

I think that is the intention here. That is to replace the default value not to actually remove 'auto' from possible gamma values.
Does the usage of gamma=auto_deprecate sounds misleading here? Or better simply use auto_default? Though @jnothman suggested that here #8535 (comment) . Also I think the warning message should be made more clear. WDYT?

If gamma is 'auto' then 1/n_features will be used instead.
If gamma is 'auto' then 1/n_features will be used.
If gamma is 'scale' then 1/(n_features * X.std()) will be used.
The current default 'auto' is deprecated in version 0.20 and will
Copy link
Member

Choose a reason for hiding this comment

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

I guess the formulation here was what led me to think that 'auto' is deprecated, i.e. will be removed. I would rather say that "The default value will change to 'scale' in 0.22". I'm not sure about a good way to say when this decision was made. I think we had "This warning was introduced in version X" somewhere.

@jnothman
Copy link
Member

jnothman commented Dec 18, 2017 via email

@gxyd
Copy link
Contributor Author

gxyd commented Dec 19, 2017 via email

@amueller
Copy link
Member

There's probably no literature on why this would be good. But the 1/n_features heuristic only makes sense when X is scaled. If you scaled X, there will be no change. If you didn't scale X, it'll "fix" that somewhat. Clearly, gamma is inversely related to scaling of the data; if you scale X by 10 you need to scale gamma by 1/10.
Why not change it? Because that'll change the behavior of existing code and people will suddenly get different results.

@gxyd
Copy link
Contributor Author

gxyd commented Dec 19, 2017

I had a look over "A Practical Guide to Support Vector Classification" (by Chih-Wei Hsu, Chih-Chung Chang, and Chih-Jen Lin), and it says that

Scaling before applying SVM is very important. Part 2 of Sarle’s Neural Networks
FAQ Sarle (1997) explains the importance of this and most of considerations also ap-
ply to SVM.

So considering that, it would imply that in most of the cases scaling is mostly needed.

Why not change it? Because that'll change the behavior of existing code and people will suddenly get different results.

Yes, I totally understand this part. "Changing" of things is one of the things to upset users.

Though I'll take your final word, if you still consider that the change is worth it.

@amueller
Copy link
Member

So considering that, it would imply that in most of the cases scaling is mostly needed.

Yes, but people don't do it. Basically the new default helps people who forget to scale their data.

@gxyd
Copy link
Contributor Author

gxyd commented Dec 19, 2017

That is right. Should I close the PR then? May be the corresponding issue as well? (I can't close the issue, I didn't open).

@amueller
Copy link
Member

What? No. Please go ahead with this. I would do the full deprecation cycle as you did.

@amueller
Copy link
Member

I would just clarify the wording on what exactly changes.

@gxyd
Copy link
Contributor Author

gxyd commented Dec 20, 2017 via email

@gxyd
Copy link
Contributor Author

gxyd commented Dec 20, 2017

I don't think the error is related to the PR. It raises

CondaValueError: invalid package specification: python=

So possibly something wrong with the build configuration itself.

@gxyd
Copy link
Contributor Author

gxyd commented Dec 20, 2017

@amueller any suggestion for the failing tests?

@amueller
Copy link
Member

Ignore it ;)

ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jul 25, 2019
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Aug 22, 2019
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Oct 5, 2019
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Oct 11, 2019
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Nov 1, 2019
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jan 10, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jan 30, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jan 30, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Feb 27, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Mar 12, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Apr 17, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request May 3, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request May 30, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jul 5, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jul 5, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Aug 5, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Oct 15, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Nov 28, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Dec 12, 2020
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Feb 25, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Feb 25, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Mar 27, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jun 15, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jul 23, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Nov 10, 2021
ivannz added a commit to ivannz/scikit-learn that referenced this pull request May 15, 2022
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Jun 14, 2022
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Aug 29, 2022
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Sep 5, 2022
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.

gamma="auto" in SVC
7 participants