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

Skip to content

Commit bbedeef

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 174d4b2 commit bbedeef

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
@@ -1808,16 +1808,6 @@ class SVDD(OutlierMixin, BaseLibSVM):
18081808
support_vectors_ : ndarray of shape (n_SV, n_features)
18091809
Support vectors.
18101810
1811-
Examples
1812-
--------
1813-
>>> from sklearn.svm import SVDD
1814-
>>> X = [[0], [0.44], [0.45], [0.46], [1]]
1815-
>>> clf = SVDD(gamma='auto').fit(X)
1816-
>>> clf.predict(X)
1817-
array([-1, 1, 1, 1, -1])
1818-
>>> clf.score_samples(X)
1819-
array([0.5298..., 0.8047..., 0.8056..., 0.8061..., 0.4832...])
1820-
18211811
See Also
18221812
--------
18231813
OneClassSVM : Support vector method for outlier detection via a separating
@@ -1834,47 +1824,82 @@ class SVDD(OutlierMixin, BaseLibSVM):
18341824
to support vector data description (SVDD)." Technical
18351825
Report, Department of Computer Science, National Taiwan
18361826
University.
1827+
1828+
Examples
1829+
--------
1830+
>>> from sklearn.svm import SVDD
1831+
>>> X = [[0], [0.44], [0.45], [0.46], [1]]
1832+
>>> clf = SVDD(gamma='auto').fit(X)
1833+
>>> clf.predict(X)
1834+
array([-1, 1, 1, 1, -1])
1835+
>>> clf.score_samples(X)
1836+
array([0.5298..., 0.8047..., 0.8056..., 0.8061..., 0.4832...])
18371837
"""
18381838

1839-
_impl = 'svdd_l1'
1839+
_impl = "svdd_l1"
18401840

1841-
def __init__(self, *, kernel='rbf', degree=3, gamma='scale',
1842-
coef0=0.0, tol=1e-3, nu=0.5, shrinking=True, cache_size=200,
1843-
verbose=False, max_iter=-1):
1841+
def __init__(
1842+
self,
1843+
*,
1844+
kernel="rbf",
1845+
degree=3,
1846+
gamma="scale",
1847+
coef0=0.0,
1848+
tol=1e-3,
1849+
nu=0.5,
1850+
shrinking=True,
1851+
cache_size=200,
1852+
verbose=False,
1853+
max_iter=-1,
1854+
):
18441855

18451856
super().__init__(
1846-
kernel=kernel, degree=degree, gamma=gamma, coef0=coef0,
1847-
tol=tol, C=0., nu=nu, epsilon=0., shrinking=shrinking,
1848-
probability=False, cache_size=cache_size, class_weight=None,
1849-
verbose=verbose, max_iter=max_iter, random_state=None)
1857+
kernel=kernel,
1858+
degree=degree,
1859+
gamma=gamma,
1860+
coef0=coef0,
1861+
tol=tol,
1862+
C=0.0,
1863+
nu=nu,
1864+
epsilon=0.0,
1865+
shrinking=shrinking,
1866+
probability=False,
1867+
cache_size=cache_size,
1868+
class_weight=None,
1869+
verbose=verbose,
1870+
max_iter=max_iter,
1871+
random_state=None,
1872+
)
18501873

18511874
def fit(self, X, y=None, sample_weight=None, **params):
1852-
"""Learns the soft minimum volume hypersphere around the sample X.
1875+
"""Learn a soft minimum-volume hypersphere around the sample X.
18531876
18541877
Parameters
18551878
----------
18561879
X : {array-like, sparse matrix} of shape (n_samples, n_features)
18571880
Set of samples, where n_samples is the number of samples and
18581881
n_features is the number of features.
18591882
1883+
y : Ignored
1884+
Not used, present for API consistency by convention.
1885+
18601886
sample_weight : array-like of shape (n_samples,), default=None
18611887
Per-sample weights. Rescale C per sample. Higher weights
18621888
force the classifier to put more emphasis on these points.
18631889
1864-
y : Ignored
1865-
not used, present for API consistency by convention.
1890+
**params : dict
1891+
Additional fit parameters.
18661892
18671893
Returns
18681894
-------
18691895
self : object
1896+
Fitted estimator.
18701897
18711898
Notes
18721899
-----
18731900
If X is not a C-ordered contiguous array it is copied.
1874-
18751901
"""
1876-
super().fit(X, np.ones(_num_samples(X)),
1877-
sample_weight=sample_weight, **params)
1902+
super().fit(X, np.ones(_num_samples(X)), sample_weight=sample_weight, **params)
18781903
self.offset_ = -self._intercept_
18791904
return self
18801905

@@ -1932,8 +1957,9 @@ def predict(self, X):
19321957

19331958
def _more_tags(self):
19341959
return {
1935-
'_xfail_checks': {
1936-
'check_sample_weights_invariance':
1937-
'zero sample_weight is not equivalent to removing samples',
1960+
"_xfail_checks": {
1961+
"check_sample_weights_invariance": (
1962+
"zero sample_weight is not equivalent to removing samples"
1963+
),
19381964
}
19391965
}

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
@@ -335,20 +337,22 @@ def test_sparse_oneclasssvm(datasets_index, kernel):
335337

336338

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

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

354358

sklearn/svm/tests/test_svm.py

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

319319
def test_svdd():
320320
# Test the output of libsvm for the SVDD problem with default parameters
321-
clf = svm.SVDD(gamma='scale')
321+
clf = svm.SVDD(gamma="scale")
322322
clf.fit(X)
323323
pred = clf.predict(T)
324324

325325
assert_array_equal(pred, [+1, -1, -1])
326-
assert pred.dtype == np.dtype('intp')
326+
assert pred.dtype == np.dtype("intp")
327327
assert_array_almost_equal(clf.intercept_, [0.2817], decimal=3)
328-
assert_array_almost_equal(clf.dual_coef_,
329-
[[0.7500, 0.7499, 0.7499, 0.7500]],
330-
decimal=3)
328+
assert_array_almost_equal(
329+
clf.dual_coef_, [[0.7500, 0.7499, 0.7499, 0.7500]], decimal=3
330+
)
331331
assert not hasattr(clf, "coef_")
332332

333333

@@ -351,15 +351,15 @@ def test_svdd_decision_function():
351351
X_outliers = rnd.uniform(low=-4, high=4, size=(20, 2))
352352

353353
# fit the model
354-
clf = svm.SVDD(gamma='scale', nu=0.1,
355-
kernel="poly", degree=2, coef0=1.0).fit(X_train)
354+
clf = svm.SVDD(gamma="scale", nu=0.1, kernel="poly", degree=2, coef0=1.0)
355+
clf.fit(X_train)
356356

357357
# predict and validate things
358358
y_pred_test = clf.predict(X_test)
359-
assert np.mean(y_pred_test == 1) > .9
359+
assert np.mean(y_pred_test == 1) > 0.9
360360

361361
y_pred_outliers = clf.predict(X_outliers)
362-
assert np.mean(y_pred_outliers == -1) > .65
362+
assert np.mean(y_pred_outliers == -1) > 0.65
363363

364364
dec_func_test = clf.decision_function(X_test)
365365
assert_array_equal((dec_func_test > 0).ravel(), y_pred_test == 1)
@@ -390,28 +390,30 @@ def test_svdd_score_samples():
390390
X_train = np.r_[X + 2, X - 2]
391391

392392
# Evaluate the scores on a small uniform 2-d mesh
393-
xx, yy = np.meshgrid(np.linspace(-5, 5, num=26),
394-
np.linspace(-5, 5, num=26))
393+
xx, yy = np.meshgrid(np.linspace(-5, 5, num=26), np.linspace(-5, 5, num=26))
395394
X_test = np.c_[xx.ravel(), yy.ravel()]
396395

397396
# Fit the model for at least 10% support vectors
398-
clf = svm.SVDD(nu=0.1, kernel="poly", gamma='scale', degree=2, coef0=1.0)
397+
clf = svm.SVDD(nu=0.1, kernel="poly", gamma="scale", degree=2, coef0=1.0)
399398
clf.fit(X_train)
400399

401400
# Check score_samples() implementation
402-
assert_array_almost_equal(clf.score_samples(X_test),
403-
clf.decision_function(X_test) + clf.offset_)
401+
assert_array_almost_equal(
402+
clf.score_samples(X_test), clf.decision_function(X_test) + clf.offset_
403+
)
404404

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

408408
assert_almost_equal(clf._gamma, gamma)
409409

410410
# Compute the kernel matrices
411-
k_zx = polynomial_kernel(X_train[clf.support_], X_test,
412-
gamma=gamma, degree=clf.degree, coef0=clf.coef0)
413-
k_xx = polynomial_kernel(X_test, gamma=gamma,
414-
degree=clf.degree, coef0=clf.coef0).diagonal()
411+
k_zx = polynomial_kernel(
412+
X_train[clf.support_], X_test, gamma=gamma, degree=clf.degree, coef0=clf.coef0
413+
)
414+
k_xx = polynomial_kernel(
415+
X_test, gamma=gamma, degree=clf.degree, coef0=clf.coef0
416+
).diagonal()
415417

416418
# Compute the sample scores = decision scores without `-\rho`
417419
scores_ = np.dot(clf.dual_coef_, k_zx - k_xx[np.newaxis] / 2).ravel()
@@ -451,8 +453,7 @@ def test_oneclass_and_svdd():
451453
assert_array_almost_equal(svdd.intercept_, svdd_intercept, decimal=3)
452454

453455
# Evaluate the decision function on a uniformly spaced 2-d mesh
454-
xx, yy = np.meshgrid(np.linspace(-5, 5, num=101),
455-
np.linspace(-5, 5, num=101))
456+
xx, yy = np.meshgrid(np.linspace(-5, 5, num=101), np.linspace(-5, 5, num=101))
456457
mesh = np.c_[xx.ravel(), yy.ravel()]
457458

458459
svdd_df = svdd.decision_function(mesh)
@@ -1142,7 +1143,7 @@ def test_immutable_coef_property():
11421143
svm.SVR(kernel="linear").fit(iris.data, iris.target),
11431144
svm.NuSVR(kernel="linear").fit(iris.data, iris.target),
11441145
svm.OneClassSVM(kernel="linear").fit(iris.data),
1145-
svm.SVDD(kernel='linear').fit(iris.data),
1146+
svm.SVDD(kernel="linear").fit(iris.data),
11461147
]
11471148
for clf in svms:
11481149
with pytest.raises(AttributeError):

0 commit comments

Comments
 (0)
0