8000 FIX: Updated the default gamma to reflect #10331 and tests, fixed the… · ivannz/scikit-learn@2213613 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2213613

Browse files
committed
FIX: Updated the default gamma to reflect scikit-learn#10331 and tests, fixed the docstring parameter order
1 parent 96c18f6 commit 2213613

File tree

3 files changed

+31
-19
lines changed

3 files changed

+31
-19
lines changed

sklearn/svm/_classes.py

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,18 +1341,19 @@ class SVDD(BaseLibSVM, OutlierMixin):
13411341
If none is given, 'rbf' will be used. If a callable is given it is
13421342
used to precompute the kernel matrix.
13431343
1344-
nu : float, optional
1345-
An upper bound on the fraction of training errors and a lower bound
1346-
on the fraction of support vectors. Should be in the interval (0, 1].
1347-
By default 0.5 will be taken.
1348-
13491344
degree : int, optional (default=3)
13501345
Degree of the polynomial kernel function ('poly').
13511346
Ignored by all other kernels.
13521347
13531348
gamma : float, optional (default='auto')
13541349
Kernel coefficient for 'rbf', 'poly' and 'sigmoid'.
1355-
If gamma is 'auto' then 1/n_features will be used instead.
1350+
1351+
Current default is 'auto' which uses 1 / n_features,
1352+
if ``gamma='scale'`` is passed then it uses 1 / (n_features * X.std())
1353+
as value of gamma. The current default of gamma, 'auto', will change
1354+
to 'scale' in version 0.22. 'auto_deprecated', a deprecated version of
1355+
'auto' is used as a default indicating that no explicit value of gamma
1356+
was passed.
13561357
13571358
coef0 : float, optional (default=0.0)
13581359
Independent term in kernel function.
@@ -1361,6 +1362,11 @@ class SVDD(BaseLibSVM, OutlierMixin):
13611362
tol : float, optional
13621363
Tolerance for stopping criterion.
13631364
1365+
nu : float, optional
1366+
An upper bound on the fraction of training errors and a lower bound
1367+
on the fraction of support vectors. Should be in the interval (0, 1].
1368+
By default 0.5 will be taken.
1369+
13641370
shrinking : boolean, optional
13651371
Whether to use the shrinking heuristic.
13661372
@@ -1416,8 +1422,8 @@ class SVDD(BaseLibSVM, OutlierMixin):
14161422

14171423
_impl = 'svdd_l1'
14181424

1419-
def __init__(self, kernel='rbf', degree=3, gamma='auto', coef0=0.0,
1420-
tol=1e-3, nu=0.5, shrinking=True, cache_size=200,
1425+
def __init__(self, kernel='rbf', degree=3, gamma='auto_deprecated',
1426+
coef0=0.0, tol=1e-3, nu=0.5, shrinking=True, cache_size=200,
14211427
verbose=False, max_iter=-1):
14221428

14231429
super(SVDD, self).__init__(

sklearn/svm/tests/test_sparse.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -312,8 +312,8 @@ def test_sparse_svdd():
312312
kernels = ["linear", "poly", "rbf", "sigmoid"]
313313
for dataset in datasets:
314314
for kernel in kernels:
315-
clf = svm.SVDD(kernel=kernel)
316-
sp_clf = svm.SVDD(kernel=kernel)
315+
clf = svm.SVDD(gamma='scale', kernel=kernel)
316+
sp_clf = svm.SVDD(gamma='scale', kernel=kernel)
317317
check_svm_model_equal(clf, sp_clf, *dataset)
318318

319319

sklearn/svm/tests/test_svm.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -297,15 +297,15 @@ def test_oneclass_score_samples():
297297

298298
def test_svdd():
299299
# Test the output of libsvm for the SVDD problem with default parameters
300-
clf = svm.SVDD()
300+
clf = svm.SVDD(gamma='scale')
301301
clf.fit(X)
302302
pred = clf.predict(T)
303303

304304
assert_array_equal(pred, [-1, -1, -1])
305305
assert_equal(pred.dtype, np.dtype('intp'))
306-
assert_array_almost_equal(clf.intercept_, [0.491], decimal=3)
306+
assert_array_almost_equal(clf.intercept_, [0.383], decimal=3)
307307
assert_array_almost_equal(clf.dual_coef_,
308-
[[0.632, 0.233, 0.633, 0.234, 0.632, 0.633]],
308+
[[0.681, 0.139, 0.680, 0.140, 0.680, 0.680]],
309309
decimal=3)
310310
assert_false(hasattr(clf, "coef_"))
311311

@@ -330,7 +330,8 @@ def test_svdd_decision_function():
330330
X_outliers = rnd.uniform(low=-4, high=4, size=(20, 2))
331331

332332
# fit the model
333-
clf = svm.SVDD(nu=0.1, kernel="poly", degree=2, coef0=1.0).fit(X_train)
333+
clf = svm.SVDD(gamma='scale', nu=0.1,
334+
kernel="poly", degree=2, coef0=1.0).fit(X_train)
334335

335336
# predict and validate things
336337
y_pred_test = clf.predict(X_test)
@@ -373,17 +374,22 @@ def test_svdd_score_samples():
373374
X_test = np.c_[xx.ravel(), yy.ravel()]
374375

375376
# Fit the model for at least 10% support vectors
376-
clf = svm.SVDD(nu=0.1, kernel="poly", degree=2, coef0=1.0)
377+
clf = svm.SVDD(nu=0.1, kernel="poly", gamma='scale', degree=2, coef0=1.0)
377378
clf.fit(X_train)
378379

379380
# Check score_samples() implementation
380381
assert_array_almost_equal(clf.score_samples(X_test),
381382
clf.decision_function(X_test) + clf.offset_)
382383

384+
# Test the gamma="scale"
385+
gamma = 1.0 / (X.shape[1] * X_train.std())
386+
387+
assert_almost_equal(clf._gamma, gamma)
388+
383389
# Compute the kernel matrices
384390
k_zx = polynomial_kernel(X_train[clf.support_], X_test,
385-
degree=clf.degree, coef0=clf.coef0)
386-
k_xx = polynomial_kernel(X_test,
391+
gamma=gamma, degree=clf.degree, coef0=clf.coef0)
392+
k_xx = polynomial_kernel(X_test, gamma=gamma,
387393
degree=clf.degree, coef0=clf.coef0).diagonal()
388394

389395
# Compute the sample scores = decision scores without `-\rho`
@@ -405,10 +411,10 @@ def test_oneclass_and_svdd():
405411
# Test the output of libsvm for the SVDD and the One-Class SVM
406412
nu = 0.15
407413

408-
svdd = svm.SVDD(nu=nu, kernel="rbf")
414+
svdd = svm.SVDD(nu=nu, kernel="rbf", gamma="scale")
409415
svdd.fit(X_train)
410416

411-
ocsvm = svm.OneClassSVM(nu=nu, kernel="rbf")
417+
ocsvm = svm.OneClassSVM(nu=nu, kernel="rbf", gamma="scale")
412418
ocsvm.fit(X_train)
413419

414420
# The intercept of the SVDD differs from that of the One-Class SVM:

0 commit comments

Comments
 (0)
0