Skip to content

FIX Correct the definition of gamma=scale in svm #13221

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 7 commits into from
Feb 24, 2019
Merged

FIX Correct the definition of gamma=scale in svm #13221

merged 7 commits into from
Feb 24, 2019

Conversation

qinhanmin2014
Copy link
Member

Closes #12741
See #13186 (comment):
And I'm wondering whether it's possible to solve #12741 in 0.20.3 by changing the definition of gamma='scale' directly, since it's introduced erroneously in 0.20. This is an embarrassing mistake and I think it'll be much more difficult to solve it in 0.21.X (maybe at that time we'll need to deprecate scale and introduce another option).

@@ -1710,19 +1710,23 @@ def test_deprecated_grid_search_iid():
depr_message = ("The default of the `iid` parameter will change from True "
"to False in version 0.22")
X, y = make_blobs(n_samples=54, random_state=0, centers=2)
grid = GridSearchCV(SVC(gamma='scale'), param_grid={'C': [1]}, cv=3)
grid = GridSearchCV(SVC(gamma='scale', random_state=0),
Copy link
Member Author

Choose a reason for hiding this comment

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

We do not always warn.

if self.iid == 'warn':
warn = False
for scorer_name in scorers.keys():
scores = test_scores[scorer_name].reshape(n_candidates,
n_splits)
means_weighted = np.average(scores, axis=1,
weights=test_sample_counts)
means_unweighted = np.average(scores, axis=1)
if not np.allclose(means_weighted, means_unweighted,
rtol=1e-4, atol=1e-4):
warn = True
break
if warn:
warnings.warn("The default of the `iid` parameter will change "
"from True to False in version 0.22 and will be"
" removed in 0.24. This will change numeric"
" results when test-set sizes are unequal.",
DeprecationWarning)

@@ -87,9 +87,9 @@ def test_svc():
kernels = ["linear", "poly", "rbf", "sigmoid"]
for dataset in datasets:
for kernel in kernels:
clf = svm.SVC(gamma='scale', kernel=kernel, probability=True,
clf = svm.SVC(gamma=1, kernel=kernel, probability=True,
Copy link
Member Author

Choose a reason for hiding this comment

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

numerical issue here and below

X = np.array([[-2, -1], [-1, -1], [-1, -2], [1, 1], [1, 2], [2, 1]])
X_sp = sparse.lil_matrix(X)
Y = [1, 1, 1, 2, 2, 2]
clf = svm.SVC(gamma='scale', kernel=kernel, probability=True,
              random_state=0, decision_function_shape='ovo')
sp_clf = svm.SVC(gamma='scale', kernel=kernel, probability=True,
                 random_state=0, decision_function_shape='ovo')
clf.fit(X, Y)
print(clf._gamma)
print(clf.support_)
sp_clf.fit(X_sp, Y)
print(sp_clf._gamma)
print(sp_clf.support_)
# 0.25
# [0 1 3 4]
# 0.25000000000000006
# [1 2 3 5]

@@ -344,7 +344,7 @@ once will overwrite what was learned by any previous ``fit()``::
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
>>> clf.predict(X_test)
array([1, 0, 1, 1, 0])
array([0, 0, 0, 1, 0])
Copy link
Member Author

Choose a reason for hiding this comment

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

these samples are generated randomly (maybe we should avoid doing so in the tutorial?)

@qinhanmin2014
Copy link
Member Author

Ready for a review @jnothman @amueller
We need to change some tests, see my reviews above.
Tagging 0.20.3.

@qinhanmin2014 qinhanmin2014 added this to the 0.20.3 milestone Feb 22, 2019
Copy link
Member

@ogrisel ogrisel left a comment

Choose a reason for hiding this comment

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

LGTM. But because gamma is now a fitted parameter, wouldn't it make sense to expose the estimated bandwidth as a public attribute gamma_ instead of a private attribute _gamma?

I would be +0 for making it public.

@amueller
Copy link
Member

lgtm (but github doesn't let me click approve?)

@qinhanmin2014
Copy link
Member Author

these samples are generated randomly (maybe we should avoid doing so in the tutorial?)

Will soon open a PR to update the tutorial.

But because gamma is now a fitted parameter, wouldn't it make sense to expose the estimated bandwidth as a public attribute gamma_ instead of a private attribute _gamma?

Also +0 from my side and this is out of the scope of this PR. Maybe open an issue/PR if someone wants it?

@jnothman jnothman merged commit 5c8167a into scikit-learn:master Feb 24, 2019
jnothman pushed a commit to jnothman/scikit-learn that referenced this pull request Feb 24, 2019
@qinhanmin2014 qinhanmin2014 deleted the svm-gamma branch February 24, 2019 07:44
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Mar 19, 2019
ivannz added a commit to ivannz/scikit-learn that referenced this pull request Apr 23, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
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.

4 participants