8000 migrate svdd code style to Black (#18948) · ivannz/scikit-learn@b379ea6 · GitHub
[go: up one dir, main page]

Skip to content

Commit b379ea6

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 e6d5049 commit b379ea6

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
@@ -24,8 +24,7 @@
2424
from ..exceptions import NotFittedError
2525

2626

27-
LIBSVM_IMPL = ['c_svc', 'nu_svc', 'one_class', 'epsilon_svr', 'nu_svr',
28-
'svdd_l1']
27+
LIBSVM_IMPL = ["c_svc", "nu_svc", "one_class", "epsilon_svr", "nu_svr", "svdd_l1"]
2928

3029

3130
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
@@ -1752,16 +1752,6 @@ class SVDD(OutlierMixin, BaseLibSVM):
17521752
support_vectors_ : ndarray of shape (n_SV, n_features)
17531753
Support vectors.
17541754
1755-
Examples
1756-
--------
1757-
>>> from sklearn.svm import SVDD
1758-
>>> X = [[0], [0.44], [0.45], [0.46], [1]]
1759-
>>> clf = SVDD(gamma='auto').fit(X)
1760-
>>> clf.predict(X)
1761-
array([-1, 1, 1, 1, -1])
1762-
>>> clf.score_samples(X)
1763-
array([0.5298..., 0.8047..., 0.8056..., 0.8061..., 0.4832...])
1764-
17651755
See Also
17661756
--------
17671757
OneClassSVM : Support vector method for outlier detection via a separating
@@ -1778,47 +1768,82 @@ class SVDD(OutlierMixin, BaseLibSVM):
17781768
to support vector data description (SVDD)." Technical
17791769
Report, Department of Computer Science, National Taiwan
17801770
University.
1771+
1772+
Examples
1773+
--------
1774+
>>> from sklearn.svm import SVDD
1775+
>>> X = [[0], [0.44], [0.45], [0.46], [1]]
1776+
>>> clf = SVDD(gamma='auto').fit(X)
1777+
>>> clf.predict(X)
1778+
array([-1, 1, 1, 1, -1])
1779+
>>> clf.score_samples(X)
1780+
array([0.5298..., 0.8047..., 0.8056..., 0.8061..., 0.4832...])
17811781
"""
17821782

1783-
_impl = 'svdd_l1'
1783+
_impl = "svdd_l1"
17841784

1785-
def __init__(self, *, kernel='rbf', degree=3, gamma='scale',
1786-
coef0=0.0, tol=1e-3, nu=0.5, shrinking=True, cache_size=200,
1787-
verbose=False, max_iter=-1):
1785+
def __init__(
1786+
self,
1787+
*,
1788+
kernel="rbf",
1789+
degree=3,
1790+
gamma="scale",
1791+
coef0=0.0,
1792+
tol=1e-3,
1793+
nu=0.5,
1794+
shrinking=True,
1795+
cache_size=200,
1796+
verbose=False,
1797+
max_iter=-1,
1798+
):
17881799

17891800
super().__init__(
1790-
kernel=kernel, degree=degree, gamma=gamma, coef0=coef0,
1791-
tol=tol, C=0., nu=nu, epsilon=0., shrinking=shrinking,
1792-
probability=False, cache_size=cache_size, class_weight=None,
1793-
verbose=verbose, max_iter=max_iter, random_state=None)
1801+
kernel=kernel,
1802+
degree=degree,
1803+
gamma=gamma,
1804+
coef0=coef0,
1805+
tol=tol,
1806+
C=0.0,
1807+
nu=nu,
1808+
epsilon=0.0,
1809+
shrinking=shrinking,
1810+
probability=False,
1811+
cache_size=cache_size,
1812+
class_weight=None,
1813+
verbose=verbose,
1814+
max_iter=max_iter,
1815+
random_state=None,
1816+
)
17941817

17951818
def fit(self, X, y=None, sample_weight=None, **params):
1796-
"""Learns the soft minimum volume hypersphere around the sample X.
1819+
"""Learn a soft minimum-volume hypersphere around the sample X.
17971820
17981821
Parameters
17991822
----------
18001823
X : {array-like, sparse matrix} of shape (n_samples, n_features)
18011824
Set of samples, where n_samples is the number of samples and
18021825
n_features is the number of features.
18031826
1827+
y : Ignored
1828+
Not used, present for API consistency by convention.
1829+
18041830
sample_weight : array-like of shape (n_samples,), default=None
18051831
Per-sample weights. Rescale C per sample. Higher weights
18061832
force the classifier to put more emphasis on these points.
18071833
1808-
y : Ignored
1809-
not used, present for API consistency by convention.
1834+
**params : dict
1835+
Additional fit parameters.
18101836
18111837
Returns
18121838
-------
18131839
self : object
1840+
Fitted estimator.
18141841
18151842
Notes
18161843
-----
18171844
If X is not a C-ordered contiguous array it is copied.
1818-
18191845
"""
1820-
super().fit(X, np.ones(_num_samples(X)),
1821-
sample_weight=sample_weight, **params)
1846+
super().fit(X, np.ones(_num_samples(X)), sample_weight=sample_weight, **params)
18221847
self.offset_ = -self._intercept_
18231848
return self
18241849

@@ -1876,8 +1901,9 @@ def predict(self, X):
18761901

18771902
def _more_tags(self):
18781903
return {
1879-
'_xfail_checks': {
1880-
'check_sample_weights_invariance':
1881-
'zero sample_weight is not equivalent to removing samples',
1904+
"_xfail_checks": {
1905+
"check_sample_weights_invariance": (
1906+
"zero sample_weight is not equivalent to removing samples"
1907+
),
18821908
}
18831909
}

sklearn/svm/tests/test_sparse.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,8 @@ def check_svm_model_equal(dense_svm, sparse_svm, X_train, y_train, X_test):
7878
)
7979
if isinstance(dense_svm, svm.OneClassSVM):
8080
msg = "cannot use sparse input in 'OneClassSVM' trained on dense data"
81+
elif isinstance(dense_svm, svm.SVDD):
82+
msg = "cannot use sparse input in 'SVDD' trained on dense data"
8183
else:
8284
assert_array_almost_equal(
8385
dense_svm.predict_proba(X_test_dense), sparse_svm.predict_proba(X_test), 4
@@ -339,20 +341,22 @@ def test_sparse_oneclasssvm(datasets_index, kernel):
339341

340342

341343
def test_sparse_svdd():
342-
"""Check that sparse SVDD gives the same result as dense SVDD
343-
"""
344+
"""Check that sparse SVDD gives the same result as dense SVDD"""
344345
# many class dataset:
345346
X_blobs, _ = make_blobs(n_samples=100, centers=10, random_state=0)
346347
X_blobs = sparse.csr_matrix(X_blobs)
347348

348-
datasets = [[X_sp, None, T], [X2_sp, None, T2],
349-
[X_blobs[:80], None, X_blobs[80:]],
350-
[iris.data, None, iris.data]]
349+
datasets = [
350+
[X_sp, None, T],
351+
[X2_sp, None, T2],
352+
[X_blobs[:80], None F987 , X_blobs[80:]],
353+
[iris.data, None, iris.data],
354+
]
351355
kernels = ["linear", "poly", "rbf", "sigmoid"]
352356
for dataset in datasets:
353357
for kernel in kernels:
354-
clf = svm.SVDD(gamma='scale', kernel=kernel)
355-
sp_clf = svm.SVDD(gamma='scale', kernel=kernel)
358+
clf = svm.SVDD(gamma="scale", kernel=kernel)
359+
sp_clf = svm.SVDD(gamma="scale", kernel=kernel)
356360
check_svm_model_equal(clf, sp_clf, *dataset)
357361

358362

sklearn/svm/tests/test_svm.py

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -301,16 +301,16 @@ def test_oneclass_score_samples():
301301

302302
def test_svdd():
303303
# Test the output of libsvm for the SVDD problem with default parameters
304-
clf = svm.SVDD(gamma='scale')
304+
clf = svm.SVDD(gamma="scale")
305305
clf.fit(X)
306306
pred = clf.predict(T)
307307

308308
assert_array_equal(pred, [+1, -1, -1])
309-
assert pred.dtype == np.dtype('intp')
309+
assert pred.dtype == np.dtype("intp")
310310
assert_array_almost_equal(clf.intercept_, [0.2817], decimal=3)
311-
assert_array_almost_equal(clf.dual_coef_,
312-
[[0.7500, 0.7499, 0.7499, 0.7500]],
313-
decimal=3)
311+
assert_array_almost_equal(
312+
clf.dual_coef_, [[0.7500, 0.7499, 0.7499, 0.7500]], decimal=3
313+
)
314314
assert not hasattr(clf, "coef_")
315315

316316

@@ -334,15 +334,15 @@ def test_svdd_decision_function():
334334
X_outliers = rnd.uniform(low=-4, high=4, size=(20, 2))
335335

336336
# fit the model
337-
clf = svm.SVDD(gamma='scale', nu=0.1,
338-
kernel="poly", degree=2, coef0=1.0).fit(X_train)
337+
clf = svm.SVDD(gamma="scale", nu=0.1, kernel="poly", degree=2, coef0=1.0)
338+
clf.fit(X_train)
339339

340340
# predict and validate things
341341
y_pred_test = clf.predict(X_test)
342-
assert np.mean(y_pred_test == 1) > .9
342+
assert np.mean(y_pred_test == 1) > 0.9
343343

344344
y_pred_outliers = clf.predict(X_outliers)
345-
assert np.mean(y_pred_outliers == -1) > .65
345+
assert np.mean(y_pred_outliers == -1) > 0.65
346346

347347
dec_func_test = clf.decision_function(X_test)
348348
assert_array_equal((dec_func_test > 0).ravel(), y_pred_test == 1)
@@ -373,28 +373,30 @@ def test_svdd_score_samples():
373373
X_train = np.r_[X + 2, X - 2]
374374

375375
# Evaluate the scores on a small uniform 2-d mesh
376-
xx, yy = np.meshgrid(np.linspace(-5, 5, num=26),
377-
np.linspace(-5, 5, num=26))
376+
xx, yy = np.meshgrid(np.linspace(-5, 5, num=26), np.linspace(-5, 5, num=26))
378377
X_test = np.c_[xx.ravel(), yy.ravel()]
379378

380379
# Fit the model for at least 10% support vectors
381-
clf = svm.SVDD(nu=0.1, kernel="poly", gamma='scale', degree=2, coef0=1.0)
380+
clf = svm.SVDD(nu=0.1, kernel="poly", gamma="scale", degree=2, coef0=1.0)
382381
clf.fit(X_train)
383382

384383
# Check score_samples() implementation
385-
assert_array_almost_equal(clf.score_samples(X_test),
386-
clf.decision_function(X_test) + clf.offset_)
384+
assert_array_almost_equal(
385+
clf.score_samples(X_test), clf.decision_function(X_test) + clf.offset_
386+
)
387387

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

391391
assert_almost_equal(clf._gamma, gamma)
392392

393393
# Compute the kernel matrices
394-
k_zx = polynomial_kernel(X_train[clf.support_], X_test,
395-
gamma=gamma, degree=clf.degree, coef0=clf.coef0)
396-
k_xx = polynomial_kernel(X_test, gamma=gamma,
397-
degree=clf.degree, coef0=clf.coef0).diagonal()
394+
k_zx = polynomial_kernel(
395+
X_train[clf.support_], X_test, gamma=gamma, degree=clf.degree, coef0=clf.coef0
396+
)
397+
k_xx = polynomial_kernel(
398+
X_test, gamma=gamma, degree=clf.degree, coef0=clf.coef0
399+
).diagonal()
398400

399401
# Compute the sample scores = decision scores without `-\rho`
400402
scores_ = np.dot(clf.dual_coef_, k_zx - k_xx[np.newaxis] / 2).ravel()
@@ -434,8 +436,7 @@ def test_oneclass_and_svdd():
434436
assert_array_almost_equal(svdd.intercept_, svdd_intercept, decimal=3)
435437

436438
# Evaluate the decision function on a uniformly spaced 2-d mesh
437-
xx, yy = np.meshgrid(np.linspace(-5, 5, num=101),
438-
np.linspace(-5, 5, num=101))
439+
xx, yy = np.meshgrid(np.linspace(-5, 5, num=101), np.linspace(-5, 5, num=101))
439440
mesh = np.c_[xx.ravel(), yy.ravel()]
440441

441442
svdd_df = svdd.decision_function(mesh)
@@ -1089,7 +1090,7 @@ def test_immutable_coef_property():
10891090
svm.SVR(kernel="linear").fit(iris.data, iris.target),
10901091
svm.NuSVR(kernel="linear").fit(iris.data, iris.target),
10911092
svm.OneClassSVM(kernel="linear").fit(iris.data),
1092-
svm.SVDD(kernel='linear').fit(iris.data),
1093+
svm.SVDD(kernel="linear").fit(iris.data),
10931094
]
10941095
for clf in svms:
10951096
with pytest.raises(AttributeError):

0 commit comments

Comments
 (0)
0