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

Skip to content

Commit b0f4926

Browse files
committed
migrate svdd code style to Black (scikit-learn#18948)
ensure SVDD passes numpydoc validation (scikit-learn#20463) check for svdd in `test_sparse.py:check_svm_model_equal` to avoid calling `.predict_proba`
1 parent fd43605 commit b0f4926

File tree

5 files changed

+88
-59
lines changed

5 files changed

+88
-59
lines changed

sklearn/svm/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
# of their respective owners.
1111
# License: BSD 3 clause (C) INRIA 2010
1212

13-
from ._classes import SVC, NuSVC, SVR, NuSVR, OneClassSVM, LinearSVC, \
14-
LinearSVR, SVDD
13+
from ._classes import SVC, NuSVC, SVR, NuSVR, OneClassSVM, LinearSVC, LinearSVR, SVDD
1514
from ._bounds import l1_min_c
1615

1716
__all__ = [

sklearn/svm/_base.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
from ..exceptions import NotFittedError
2828

2929

30-
LIBSVM_IMPL = ['c_svc', 'nu_svc', 'one_class', 'epsilon_svr', 'nu_svr',
31-
'svdd_l1']
30+
LIBSVM_IMPL = ["c_svc", "nu_svc", "one_class", "epsilon_svr", "nu_svr", "svdd_l1"]
3231

3332

3433
def _one_vs_one_coef(dual_coef, n_support, support_vectors):

sklearn/svm/_classes.py

Lines changed: 53 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1932,16 +1932,6 @@ class SVDD(OutlierMixin, BaseLibSVM):
19321932
support_vectors_ : ndarray of shape (n_SV, n_features)
19331933
Support vectors.
19341934
1935-
Examples
1936-
--------
1937-
>>> from sklearn.svm import SVDD
1938-
>>> X = [[0], [0.44], [0.45], [0.46], [1]]
1939-
>>> clf = SVDD(gamma='auto').fit(X)
1940-
>>> clf.predict(X)
1941-
array([-1, 1, 1, 1, -1])
1942-
>>> clf.score_samples(X)
1943-
array([0.5298..., 0.8047..., 0.8056..., 0.8061..., 0.4832...])
1944-
19451935
See Also
19461936
--------
19471937
OneClassSVM : Support vector method for outlier detection via a separating
@@ -1958,47 +1948,82 @@ class SVDD(OutlierMixin, BaseLibSVM):
19581948
to support vector data description (SVDD)." Technical
19591949
Report, Department of Computer Science, National Taiwan
19601950
University.
1951+
1952+
Examples
1953+
--------
1954+
>>> from sklearn.svm import SVDD
1955+
>>> X = [[0], [0.44], [0.45], [0.46], [1]]
1956+
>>> clf = SVDD(gamma='auto').fit(X)
1957+
>>> clf.predict(X)
1958+
array([-1, 1, 1, 1, -1])
1959+
>>> clf.score_samples(X)
1960+
array([0.5298..., 0.8047..., 0.8056..., 0.8061..., 0.4832...])
19611961
"""
19621962

1963-
_impl = 'svdd_l1'
1963+
_impl = "svdd_l1"
19641964

1965-
def __init__(self, *, kernel='rbf', degree=3, gamma='scale',
1966-
coef0=0.0, tol=1e-3, nu=0.5, shrinking=True, cache_size=200,
1967-
verbose=False, max_iter=-1):
1965+
def __init__(
1966+
self,
1967+
*,
1968+
kernel="rbf",
1969+
degree=3,
1970+
gamma="scale",
1971+
coef0=0.0,
1972+
tol=1e-3,
1973+
nu=0.5,
1974+
shrinking=True,
1975+
cache_size=200,
1976+
verbose=False,
1977+
max_iter=-1,
1978+
):
19681979

19691980
super().__init__(
1970-
kernel=kernel, degree=degree, gamma=gamma, coef0=coef0,
1971-
tol=tol, C=0., nu=nu, epsilon=0., shrinking=shrinking,
1972-
probability=False, cache_size=cache_size, class_weight=None,
1973-
verbose=verbose, max_iter=max_iter, random_state=None)
1981+
kernel=kernel,
1982+
degree=degree,
1983+
gamma=gamma,
1984+
coef0=coef0,
1985+
tol=tol,
1986+
C=0.0,
1987+
nu=nu,
1988+
epsilon=0.0,
1989+
shrinking=shrinking,
1990+
probability=False,
1991+
cache_size=cache_size,
1992+
class_weight=None,
1993+
verbose=verbose,
1994+
max_iter=max_iter,
1995+
random_state=None,
1996+
)
19741997

19751998
def fit(self, X, y=None, sample_weight=None, **params):
1976-
"""Learns the soft minimum volume hypersphere around the sample X.
1999+
"""Learn a soft minimum-volume hypersphere around the sample X.
19772000
19782001
Parameters
19792002
----------
19802003
X : {array-like, sparse matrix} of shape (n_samples, n_features)
19812004
Set of samples, where n_samples is the number of samples and
19822005
n_features is the number of features.
19832006
2007+
y : Ignored
2008+
Not used, present for API consistency by convention.
2009+
19842010
sample_weight : array-like of shape (n_samples,), default=None
19852011
Per-sample weights. Rescale C per sample. Higher weights
19862012
force the classifier to put more emphasis on these points.
19872013
1988-
y : Ignored
1989-
not used, present for API consistency by convention.
2014+
**params : dict
2015+
Additional fit parameters.
19902016
19912017
Returns
19922018
-------
19932019
self : object
2020+
Fitted estimator.
19942021
19952022
Notes
19962023
-----
19972024
If X is not a C-ordered contiguous array it is copied.
1998-
19992025
"""
2000-
super().fit(X, np.ones(_num_samples(X)),
2001-
sample_weight=sample_weight, **params)
2026+
super().fit(X, np.ones(_num_samples(X)), sample_weight=sample_weight, **params)
20022027
self.offset_ = -self._intercept_
20032028
return self
20042029

@@ -2056,8 +2081,9 @@ def predict(self, X):
20562081

20572082
def _more_tags(self):
20582083
return {
2059-
'_xfail_checks': {
2060-
'check_sample_weights_invariance':
2061-
'zero sample_weight is not equivalent to removing samples',
2084+
"_xfail_checks": {
2085+
"check_sample_weights_invariance": (
2086+
"zero sample_weight is not equivalent to removing samples"
2087+
),
20622088
}
20632089
}

sklearn/svm/tests/test_sparse.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ def check_svm_model_equal(dense_svm, sparse_svm, X_train, y_train, X_test):
7474
)
7575
if isinstance(dense_svm, svm.OneClassSVM):
7676
msg = "cannot use sparse input in 'OneClassSVM' trained on dense data"
77+
elif isinstance(dense_svm, svm.SVDD):
78+
msg = "cannot use sparse input in 'SVDD' trained on dense data"
7779
else:
7880
assert_array_almost_equal(
7981
dense_svm.predict_proba(X_test_dense), sparse_svm.predict_proba(X_test), 4
@@ -336,20 +338,22 @@ def test_sparse_oneclasssvm(datasets_index, kernel):
336338

337339

338340
def test_sparse_svdd():
339-
"""Check that sparse SVDD gives the same result as dense SVDD
340-
"""
341+
"""Check that sparse SVDD gives the same result as dense SVDD"""
341342
# many class dataset:
342343
X_blobs, _ = make_blobs(n_samples=100, centers=10, random_state=0)
343344
X_blobs = sparse.csr_matrix(X_blobs)
344345

345-
datasets = [[X_sp, None, T], [X2_sp, None, T2],
346-
[X_blobs[:80], None, X_blobs[80:]],
347-
[iris.data, None, iris.data]]
346+
datasets = [
347+
[X_sp, None, T],
348+
[X2_sp, None, T2],
349+
[X_blobs[:80], None, X_blobs[80:]],
350+
[iris.data, None, iris.data],
351+
]
348352
kernels = ["linear", "poly", "rbf", "sigmoid"]
349353
for dataset in datasets:
350354
for kernel in kernels:
351-
clf = svm.SVDD(gamma='scale', kernel=kernel)
352-
sp_clf = svm.SVDD(gamma='scale', kernel=kernel)
355+
clf = svm.SVDD(gamma="scale", kernel=kernel)
356+
sp_clf = svm.SVDD(gamma="scale", kernel=kernel)
353357
check_svm_model_equal(clf, sp_clf, *dataset)
354358

355359

sklearn/svm/tests/test_svm.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -364,16 +364,16 @@ def test_oneclass_fit_params_is_deprecated():
364364

365365
def test_svdd():
366366
# Test the output of libsvm for the SVDD problem with default parameters
367-
clf = svm.SVDD(gamma='scale')
367+
clf = svm.SVDD(gamma="scale")
368368
clf.fit(X)
369369
pred = clf.predict(T)
370370

371371
assert_array_equal(pred, [+1, -1, -1])
372-
assert pred.dtype == np.dtype('intp')
372+
assert pred.dtype == np.dtype("intp")
373373
assert_array_almost_equal(clf.intercept_, [0.2817], decimal=3)
374-
assert_array_almost_equal(clf.dual_coef_,
375-
[[0.7500, 0.7499, 0.7499, 0.7500]],
376-
decimal=3)
374+
assert_array_almost_equal(
375+
clf.dual_coef_, [[0.7500, 0.7499, 0.7499, 0.7500]], decimal=3
376+
)
377377
assert not hasattr(clf, "coef_")
378378

379379

@@ -397,15 +397,15 @@ def test_svdd_decision_function():
397397
X_outliers = rnd.uniform(low=-4, high=4, size=(20, 2))
398398

399399
# fit the model
400-
clf = svm.SVDD(gamma='scale', nu=0.1,
401-
kernel="poly", degree=2, coef0=1.0).fit(X_train)
400+
clf = svm.SVDD(gamma="scale", nu=0.1, kernel="poly", degree=2, coef0=1.0)
401+
clf.fit(X_train)
402402

403403
# predict and validate things
404404
y_pred_test = clf.predict(X_test)
405-
assert np.mean(y_pred_test == 1) > .9
405+
assert np.mean(y_pred_test == 1) > 0.9
406406

407407
y_pred_outliers = clf.predict(X_outliers)
408-
assert np.mean(y_pred_outliers == -1) > .65
408+
assert np.mean(y_pred_outliers == -1) > 0.65
409409

410410
dec_func_test = clf.decision_function(X_test)
411411
assert_array_equal((dec_func_test > 0).ravel(), y_pred_test == 1)
@@ -436,28 +436,30 @@ def test_svdd_score_samples():
436436
X_train = np.r_[X + 2, X - 2]
437437

438438
# Evaluate the scores on a small uniform 2-d mesh
439-
xx, yy = np.meshgrid(np.linspace(-5, 5, num=26),
440-
np.linspace(-5, 5, num=26))
439+
xx, yy = np.meshgrid(np.linspace(-5, 5, num=26), np.linspace(-5, 5, num=26))
441440
X_test = np.c_[xx.ravel(), yy.ravel()]
442441

443442
# Fit the model for at least 10% support vectors
444-
clf = svm.SVDD(nu=0.1, kernel="poly", gamma='scale', degree=2, coef0=1.0)
443+
clf = svm.SVDD(nu=0.1, kernel="poly", gamma="scale", degree=2, coef0=1.0)
445444
clf.fit(X_train)
446445

447446
# Check score_samples() implementation
448-
assert_array_almost_equal(clf.score_samples(X_test),
449-
clf.decision_function(X_test) + clf.offset_)
447+
assert_array_almost_equal(
448+
clf.score_samples(X_test), clf.decision_function(X_test) + clf.offset_
449+
)
450450

451451
# Test the gamma="scale": use .var() for scaling (c.f. issue #12741)
452452
gamma = 1.0 / (X.shape[1] * X_train.var())
453453

454454
assert_almost_equal(clf._gamma, gamma)
455455

456456
# Compute the kernel matrices
457-
k_zx = polynomial_kernel(X_train[clf.support_], X_test,
458-
gamma=gamma, degree=clf.degree, coef0=clf.coef0)
459-
k_xx = polynomial_kernel(X_test, gamma=gamma,
460-
degree=clf.degree, coef0=clf.coef0).diagonal()
457+
k_zx = polynomial_kernel(
458+
X_train[clf.support_], X_test, gamma=gamma, degree=clf.degree, coef0=clf.coef0
459+
)
460+
k_xx = polynomial_kernel(
461+
X_test, gamma=gamma, degree=clf.degree, coef0=clf.coef0
462+
).diagonal()
461463

462464
# Compute the sample scores = decision scores without `-\rho`
463465
scores_ = np.dot(clf.dual_coef_, k_zx - k_xx[np.newaxis] / 2).ravel()
@@ -497,8 +499,7 @@ def test_oneclass_and_svdd():
497499
assert_array_almost_equal(svdd.intercept_, svdd_intercept, decimal=3)
498500

499501
# Evaluate the decision function on a uniformly spaced 2-d mesh
500-
xx, yy = np.meshgrid(np.linspace(-5, 5, num=101),
501-
np.linspace(-5, 5, num=101))
502+
xx, yy = np.meshgrid(np.linspace(-5, 5, num=101), np.linspace(-5, 5, num=101))
502503
mesh = np.c_[xx.ravel(), yy.ravel()]
503504

504505
svdd_df = svdd.decision_function(mesh)
@@ -1114,7 +1115,7 @@ def test_immutable_coef_property():
11141115
svm.SVR(kernel="linear").fit(iris.data, iris.target),
11151116
svm.NuSVR(kernel="linear").fit(iris.data, iris.target),
11161117
svm.OneClassSVM(kernel="linear").fit(iris.data),
1117-
svm.SVDD(kernel='linear').fit(iris.data),
1118+
svm.SVDD(kernel="linear").fit(iris.data),
11181119
]
11191120
for clf in svms:
11201121
with pytest.raises(AttributeError):

0 commit comments

Comments
 (0)