Skip to content

Commit 41788c3

Browse files
author
Shivan Sornarajah
committed
Changed SVM to use gamma='auto' by default. Deprecated gamma=0.0.
1 parent 0a5d924 commit 41788c3

File tree

7 files changed

+69
-55
lines changed

7 files changed

+69
-55
lines changed

doc/modules/model_persistence.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ persistence model, namely `pickle <http://docs.python.org/library/pickle.html>`_
2222
>>> iris = datasets.load_iris()
2323
>>> X, y = iris.data, iris.target
2424
>>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE
25-
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
26-
kernel='rbf', max_iter=-1, probability=False, random_state=None,
27-
shrinking=True, tol=0.001, verbose=False)
25+
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
26+
gamma='auto', kernel='rbf', max_iter=-1, probability=False,
27+
random_state=None, shrinking=True, tol=0.001, verbose=False)
2828

2929
>>> import pickle
3030
>>> s = pickle.dumps(clf)

doc/modules/pipeline.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ is an estimator object::
4242
>>> clf # doctest: +NORMALIZE_WHITESPACE
4343
Pipeline(steps=[('reduce_dim', PCA(copy=True, n_components=None,
4444
whiten=False)), ('svm', SVC(C=1.0, cache_size=200, class_weight=None,
45-
coef0=0.0, degree=3, gamma=0.0, kernel='rbf', max_iter=-1,
45+
coef0=0.0, degree=3, gamma='auto', kernel='rbf', max_iter=-1,
4646
probability=False, random_state=None, shrinking=True, tol=0.001,
4747
verbose=False))])
4848

@@ -76,7 +76,7 @@ Parameters of the estimators in the pipeline can be accessed using the
7676
>>> clf.set_params(svm__C=10) # doctest: +NORMALIZE_WHITESPACE
7777
Pipeline(steps=[('reduce_dim', PCA(copy=True, n_components=None,
7878
whiten=False)), ('svm', SVC(C=10, cache_size=200, class_weight=None,
79-
coef0=0.0, degree=3, gamma=0.0, kernel='rbf', max_iter=-1,
79+
coef0=0.0, degree=3, gamma='auto', kernel='rbf', max_iter=-1,
8080
probability=False, random_state=None, shrinking=True, tol=0.001,
8181
verbose=False))])
8282

doc/modules/svm.rst

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ n_features]`` holding the training samples, and an array y of class labels
7777
>>> clf = svm.SVC()
7878
>>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE
7979
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
80-
gamma=0.0, kernel='rbf', max_iter=-1, probability=False, random_state=None,
81-
shrinking=True, tol=0.001, verbose=False)
80+
gamma='auto', kernel='rbf', max_iter=-1, probability=False,
81+
random_state=None, shrinking=True, tol=0.001, verbose=False)
8282

8383
After being fitted, the model can then be used to predict new values::
8484

@@ -116,8 +116,8 @@ classifiers are constructed and each one trains data from two classes::
116116
>>> clf = svm.SVC()
117117
>>> clf.fit(X, Y) # doctest: +NORMALIZE_WHITESPACE
118118
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
119-
gamma=0.0, kernel='rbf', max_iter=-1, probability=False, random_state=None,
120-
shrinking=True, tol=0.001, verbose=False)
119+
gamma='auto', kernel='rbf', max_iter=-1, probability=False,
120+
random_state=None, shrinking=True, tol=0.001, verbose=False)
121121
>>> dec = clf.decision_function([[1]])
122122
>>> dec.shape[1] # 4 classes: 4*3/2 = 6
123123
6
@@ -304,7 +304,7 @@ floating point values instead of integer values::
304304
>>> y = [0.5, 2.5]
305305
>>> clf = svm.SVR()
306306
>>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE
307-
SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma=0.0,
307+
SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.1, gamma='auto',
308308
kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)
309309
>>> clf.predict([[1, 1]])
310310
array([ 1.5])
@@ -504,7 +504,7 @@ test vectors must be provided.
504504
>>> gram = np.dot(X, X.T)
505505
>>> clf.fit(gram, y) # doctest: +NORMALIZE_WHITESPACE
506506
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
507-
gamma=0.0, kernel='precomputed', max_iter=-1, probability=False,
507+
gamma='auto', kernel='precomputed', max_iter=-1, probability=False,
508508
random_state=None, shrinking=True, tol=0.001, verbose=False)
509509
>>> # predict on training examples
510510
>>> clf.predict(gram)

doc/tutorial/basic/tutorial.rst

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -214,9 +214,9 @@ persistence model, namely `pickle <http://docs.python.org/library/pickle.html>`_
214214
>>> iris = datasets.load_iris()
215215
>>> X, y = iris.data, iris.target
216216
>>> clf.fit(X, y) # doctest: +NORMALIZE_WHITESPACE
217-
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
218-
kernel='rbf', max_iter=-1, probability=False, random_state=None,
219-
shrinking=True, tol=0.001, verbose=False)
217+
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
218+
gamma='auto', kernel='rbf', max_iter=-1, probability=False,
219+
random_state=None, shrinking=True, tol=0.001, verbose=False)
220220

221221
>>> import pickle
222222
>>> s = pickle.dumps(clf)
@@ -287,17 +287,17 @@ maintained::
287287
>>> iris = datasets.load_iris()
288288
>>> clf = SVC()
289289
>>> clf.fit(iris.data, iris.target)
290-
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
291-
kernel='rbf', max_iter=-1, probability=False, random_state=None,
292-
shrinking=True, tol=0.001, verbose=False)
290+
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
291+
gamma='auto', kernel='rbf', max_iter=-1, probability=False,
292+
random_state=None, shrinking=True, tol=0.001, verbose=False)
293293

294294
>>> list(clf.predict(iris.data[:3]))
295295
[0, 0, 0]
296296

297297
>>> clf.fit(iris.data, iris.target_names[iris.target])
298-
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
299-
kernel='rbf', max_iter=-1, probability=False, random_state=None,
300-
shrinking=True, tol=0.001, verbose=False)
298+
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
299+
gamma='auto', kernel='rbf', max_iter=-1, probability=False,
300+
random_state=None, shrinking=True, tol=0.001, verbose=False)
301301

302302
>>> list(clf.predict(iris.data[:3])) # doctest: +NORMALIZE_WHITESPACE
303303
['setosa', 'setosa', 'setosa']
@@ -324,16 +324,16 @@ more than once will overwrite what was learned by any previous ``fit()``::
324324

325325
>>> clf = SVC()
326326
>>> clf.set_params(kernel='linear').fit(X, y)
327-
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
328-
kernel='linear', max_iter=-1, probability=False, random_state=None,
329-
shrinking=True, tol=0.001, verbose=False)
327+
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
328+
gamma='auto', kernel='linear', max_iter=-1, probability=False,
329+
random_state=None, shrinking=True, tol=0.001, verbose=False)
330330
>>> clf.predict(X_test)
331331
array([1, 0, 1, 1, 0])
332332

333333
>>> clf.set_params(kernel='rbf').fit(X, y)
334-
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
335-
kernel='rbf', max_iter=-1, probability=False, random_state=None,
336-
shrinking=True, tol=0.001, verbose=False)
334+
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
335+
gamma='auto', kernel='rbf', max_iter=-1, probability=False,
336+
random_state=None, shrinking=True, tol=0.001, verbose=False)
337337
>>> clf.predict(X_test)
338338
array([0, 0, 0, 1, 0])
339339

doc/tutorial/statistical_inference/supervised_learning.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -453,9 +453,9 @@ classification --:class:`SVC` (Support Vector Classification).
453453
>>> from sklearn import svm
454454
>>> svc = svm.SVC(kernel='linear')
455455
>>> svc.fit(iris_X_train, iris_y_train) # doctest: +NORMALIZE_WHITESPACE
456-
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
457-
kernel='linear', max_iter=-1, probability=False, random_state=None,
458-
shrinking=True, tol=0.001, verbose=False)
456+
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
457+
gamma='auto', kernel='linear', max_iter=-1, probability=False,
458+
random_state=None, shrinking=True, tol=0.001, verbose=False)
459459

460460

461461
.. warning:: **Normalizing data**

sklearn/svm/base.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,15 @@ def __init__(self, impl, kernel, degree, gamma, coef0,
7272
if impl not in LIBSVM_IMPL: # pragma: no cover
7373
raise ValueError("impl should be one of %s, %s was given" % (
7474
LIBSVM_IMPL, impl))
75+
76+
# FIXME Remove gamma=0.0 support in 0.18
77+
if gamma == 0:
78+
msg = ("gamma=%s has been deprecated in favor of "
79+
"gamma='%s' as of 0.17. Backward compatibility"
80+
" for gamma=%s will be removed in %s")
81+
invalid_gamma = 0.0
82+
warnings.warn(msg % (invalid_gamma, "auto",
83+
invalid_gamma, "0.18"), DeprecationWarning)
7584

7685
self._impl = impl
7786
self.kernel = kernel
@@ -159,10 +168,14 @@ def fit(self, X, y, sample_weight=None):
159168
"Note: Sparse matrices cannot be indexed w/"
160169
"boolean masks (use `indices=True` in CV)."
161170
% (sample_weight.shape, X.shape))
162-
163-
if (self.kernel in ['poly', 'rbf']) and (self.gamma == 0):
171+
172+
# FIXME remove (self.gamma == 0) in 0.18
173+
if (self.kernel in ['poly', 'rbf']) and ((self.gamma == 0)
174+
or (self.gamma == 'auto')):
164175
# if custom gamma is not provided ...
165176
self._gamma = 1.0 / X.shape[1]
177+
elif self.gamma == 'auto':
178+
self._gamma = 0.0
166179
else:
167180
self._gamma = self.gamma
168181

sklearn/svm/classes.py

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -411,9 +411,9 @@ class SVC(BaseSVC):
411411
Degree of the polynomial kernel function ('poly').
412412
Ignored by all other kernels.
413413
414-
gamma : float, optional (default=0.0)
414+
gamma : float, optional (default='auto')
415415
Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
416-
If gamma is 0.0 then 1/n_features will be used instead.
416+
If gamma is 'auto' then 1/n_features will be used instead.
417417
418418
coef0 : float, optional (default=0.0)
419419
Independent term in kernel function.
@@ -489,7 +489,7 @@ class SVC(BaseSVC):
489489
>>> clf = SVC()
490490
>>> clf.fit(X, y) #doctest: +NORMALIZE_WHITESPACE
491491
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3,
492-
gamma=0.0, kernel='rbf', max_iter=-1, probability=False,
492+
gamma='auto', kernel='rbf', max_iter=-1, probability=False,
493493
random_state=None, shrinking=True, tol=0.001, verbose=False)
494494
>>> print(clf.predict([[-0.8, -1]]))
495495
[1]
@@ -506,7 +506,7 @@ class SVC(BaseSVC):
506506
507507
"""
508508

509-
def __init__(self, C=1.0, kernel='rbf', degree=3, gamma=0.0,
509+
def __init__(self, C=1.0, kernel='rbf', degree=3, gamma='auto',
510510
coef0=0.0, shrinking=True, probability=False,
511511
tol=1e-3, cache_size=200, class_weight=None,
512512
verbose=False, max_iter=-1, random_state=None):
@@ -545,9 +545,9 @@ class NuSVC(BaseSVC):
545545
Degree of the polynomial kernel function ('poly').
546546
Ignored by all other kernels.
547547
548-
gamma : float, optional (default=0.0)
548+
gamma : float, optional (default='auto')
549549
Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
550-
If gamma is 0.0 then 1/n_features will be used instead.
550+
If gamma is 'auto' then 1/n_features will be used instead.
551551
552552
coef0 : float, optional (default=0.0)
553553
Independent term in kernel function.
@@ -614,7 +614,7 @@ class NuSVC(BaseSVC):
614614
>>> from sklearn.svm import NuSVC
615615
>>> clf = NuSVC()
616616
>>> clf.fit(X, y) #doctest: +NORMALIZE_WHITESPACE
617-
NuSVC(cache_size=200, coef0=0.0, degree=3, gamma=0.0, kernel='rbf',
617+
NuSVC(cache_size=200, coef0=0.0, degree=3, gamma='auto', kernel='rbf',
618618
max_iter=-1, nu=0.5, probability=False, random_state=None,
619619
shrinking=True, tol=0.001, verbose=False)
620620
>>> print(clf.predict([[-0.8, -1]]))
@@ -630,7 +630,7 @@ class NuSVC(BaseSVC):
630630
liblinear.
631631
"""
632632

633-
def __init__(self, nu=0.5, kernel='rbf', degree=3, gamma=0.0,
633+
def __init__(self, nu=0.5, kernel='rbf', degree=3, gamma='auto',
634634
coef0=0.0, shrinking=True, probability=False,
635635
tol=1e-3, cache_size=200, verbose=False, max_iter=-1,
636636
random_state=None):
@@ -671,9 +671,9 @@ class SVR(BaseLibSVM, RegressorMixin):
671671
Degree of the polynomial kernel function ('poly').
672672
Ignored by all other kernels.
673673
674-
gamma : float, optional (default=0.0)
674+
gamma : float, optional (default='auto')
675675
Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
676-
If gamma is 0.0 then 1/n_features will be used instead.
676+
If gamma is 'auto' then 1/n_features will be used instead.
677677
678678
coef0 : float, optional (default=0.0)
679679
Independent term in kernel function.
@@ -727,7 +727,7 @@ class SVR(BaseLibSVM, RegressorMixin):
727727
>>> X = np.random.randn(n_samples, n_features)
728728
>>> clf = SVR(C=1.0, epsilon=0.2)
729729
>>> clf.fit(X, y) #doctest: +NORMALIZE_WHITESPACE
730-
SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.2, gamma=0.0,
730+
SVR(C=1.0, cache_size=200, coef0=0.0, degree=3, epsilon=0.2, gamma='auto',
731731
kernel='rbf', max_iter=-1, shrinking=True, tol=0.001, verbose=False)
732732
733733
See also
@@ -740,9 +740,9 @@ class SVR(BaseLibSVM, RegressorMixin):
740740
Scalable Linear Support Vector Machine for regression
741741
implemented using liblinear.
742742
"""
743-
def __init__(self, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, tol=1e-3,
744-
C=1.0, epsilon=0.1, shrinking=True, cache_size=200,
745-
verbose=False, max_iter=-1):
743+
def __init__(self, kernel='rbf', degree=3, gamma='auto', coef0=0.0,
744+
tol=1e-3, C=1.0, epsilon=0.1, shrinking=True,
745+
cache_size=200, verbose=False, max_iter=-1):
746746

747747
super(SVR, self).__init__(
748748
'epsilon_svr', kernel=kernel, degree=degree, gamma=gamma,
@@ -783,9 +783,9 @@ class NuSVR(BaseLibSVM, RegressorMixin):
783783
Degree of the polynomial kernel function ('poly').
784784
Ignored by all other kernels.
785785
786-
gamma : float, optional (default=0.0)
786+
gamma : float, optional (default='auto')
787787
Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
788-
If gamma is 0.0 then 1/n_features will be used instead.
788+
If gamma is 'auto' then 1/n_features will be used instead.
789789
790790
coef0 : float, optional (default=0.0)
791791
Independent term in kernel function.
@@ -839,8 +839,9 @@ class NuSVR(BaseLibSVM, RegressorMixin):
839839
>>> X = np.random.randn(n_samples, n_features)
840840
>>> clf = NuSVR(C=1.0, nu=0.1)
841841
>>> clf.fit(X, y) #doctest: +NORMALIZE_WHITESPACE
842-
NuSVR(C=1.0, cache_size=200, coef0=0.0, degree=3, gamma=0.0, kernel='rbf',
843-
max_iter=-1, nu=0.1, shrinking=True, tol=0.001, verbose=False)
842+
NuSVR(C=1.0, cache_size=200, coef0=0.0, degree=3, gamma='auto',
843+
kernel='rbf', max_iter=-1, nu=0.1, shrinking=True, tol=0.001,
844+
verbose=False)
844845
845846
See also
846847
--------
@@ -853,7 +854,7 @@ class NuSVR(BaseLibSVM, RegressorMixin):
853854
"""
854855

855856
def __init__(self, nu=0.5, C=1.0, kernel='rbf', degree=3,
856-
gamma=0.0, coef0=0.0, shrinking=True, tol=1e-3,
857+
gamma='auto', coef0=0.0, shrinking=True, tol=1e-3,
857858
cache_size=200, verbose=False, max_iter=-1):
858859

859860
super(NuSVR, self).__init__(
@@ -891,9 +892,9 @@ class OneClassSVM(BaseLibSVM):
891892
Degree of the polynomial kernel function ('poly').
892893
Ignored by all other kernels.
893894
894-
gamma : float, optional (default=0.0)
895+
gamma : float, optional (default='auto')
895896
Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
896-
If gamma is 0.0 then 1/n_features will be used instead.
897+
If gamma is 'auto' then 1/n_features will be used instead.
897898
898899
coef0 : float, optional (default=0.0)
899900
Independent term in kernel function.
@@ -942,9 +943,9 @@ class OneClassSVM(BaseLibSVM):
942943
Constants in decision function.
943944
944945
"""
945-
def __init__(self, kernel='rbf', degree=3, gamma=0.0, coef0=0.0, tol=1e-3,
946-
nu=0.5, shrinking=True, cache_size=200, verbose=False,
947-
max_iter=-1, random_state=None):
946+
def __init__(self, kernel='rbf', degree=3, gamma='auto', coef0=0.0,
947+
tol=1e-3, nu=0.5, shrinking=True, cache_size=200,
948+
verbose=False, max_iter=-1, random_state=None):
948949

949950
super(OneClassSVM, self).__init__(
950951
'one_class', kernel, degree, gamma, coef0, tol, 0., nu, 0.,

0 commit comments

Comments
 (0)