Skip to content

Commit

Permalink
update version in svdd docs to 1.1, relocate from 1.0 to 1.1 in whats…
Browse files Browse the repository at this point in the history
…_new

add backticks (scikit-learn#20914), deprecate **params in fit (scikit-learn#20843), add feature_names_in_ (scikit-learn#20787)
uncompromisingly reformat plot_oneclass_vs_svdd with black
  • Loading branch information
ivannz committed Jun 14, 2022
1 parent 5642108 commit 9ea9069
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 42 deletions.
7 changes: 0 additions & 7 deletions doc/whats_new/v1.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1213,13 +1213,6 @@ Changelog
now deprecated. Use `scipy.sparse.csgraph.shortest_path` instead. :pr:`20531`
by `Tom Dupre la Tour`_.

:mod:`sklearn.svm`
..................

- |Feature| Added the :class:`svm.SVDD` class for novelty detection based
on soft minimal volume hypersphere around the sample data. :pr:`7910`
by :user:`Ivan Nazarov <ivannz>`.

Code and Documentation Contributors
-----------------------------------

Expand Down
4 changes: 4 additions & 0 deletions doc/whats_new/v1.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,10 @@ Changelog
parameters in `fit` instead of `__init__`.
:pr:`21436` by :user:`Haidar Almubarak <Haidar13 >`.

- |Feature| Added the :class:`svm.SVDD` class for novelty detection based
on soft minimal volume hypersphere around the sample data. :pr:`7910`
by :user:`Ivan Nazarov <ivannz>`.

:mod:`sklearn.tree`
...................

Expand Down
80 changes: 49 additions & 31 deletions examples/svm/plot_oneclass_vs_svdd.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,26 @@
X_outliers = random_state.uniform(low=-4, high=4, size=(20, 2))

# Define the models
nu = .1
kernels = [("RBF", dict(kernel="rbf", gamma=0.1)),
("Poly", dict(kernel="poly", degree=2, coef0=1.0)),
]
nu = 0.1
kernels = [
("RBF", dict(kernel="rbf", gamma=0.1)),
("Poly", dict(kernel="poly", degree=2, coef0=1.0)),
]

for kernel_name, kernel in kernels:

# Use low tolerance to ensure better precision of the SVM
# optimization procedure.
classifiers = [("OCSVM", svm.OneClassSVM(nu=nu, tol=1e-8, **kernel)),
("SVDD", svm.SVDD(nu=nu, tol=1e-8, **kernel)),
]
classifiers = [
("OCSVM", svm.OneClassSVM(nu=nu, tol=1e-8, **kernel)),
("SVDD", svm.SVDD(nu=nu, tol=1e-8, **kernel)),
]

fig = plt.figure(figsize=(12, 5))
fig.suptitle("One-Class SVM versus SVDD "
"(error train, error novel regular, error novel abnormal)")
fig.suptitle(
"One-Class SVM versus SVDD "
"(error train, error novel regular, error novel abnormal)"
)

for i, (model_name, clf) in enumerate(classifiers):
clf.fit(X_train)
Expand All @@ -74,32 +78,46 @@
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)

ax.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7),
cmap=plt.cm.PuBu, zorder=-99)
ax.contourf(xx, yy, Z, levels=[0, Z.max()], colors='palevioletred',
zorder=-98)
a = ax.contour(xx, yy, Z, levels=[0], linewidths=2, colors='darkred',
zorder=-97)
ax.contourf(
xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu, zorder=-99
)
ax.contourf(xx, yy, Z, levels=[0, Z.max()], colors="palevioletred", zorder=-98)
a = ax.contour(
xx, yy, Z, levels=[0], linewidths=2, colors="darkred", zorder=-97
)

s = 40
b1 = ax.scatter(X_train[:, 0], X_train[:, 1], s=s,
c='white', edgecolors='k')
b2 = ax.scatter(X_test[:, 0], X_test[:, 1], c='blueviolet', s=s)
c = ax.scatter(X_outliers[:, 0], X_outliers[:, 1], c='gold', s=s)
ax.axis('tight')
b1 = ax.scatter(X_train[:, 0], X_train[:, 1], s=s, c="white", edgecolors="k")
b2 = ax.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s)
c = ax.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s)
ax.axis("tight")
ax.set_xlim((-6, 6))
ax.set_ylim((-6, 6))

ax.set_title("%s %s (%d/%d, %d/%d, %d/%d)"
% (model_name, kernel_name,
n_error_train, len(X_train),
n_error_test, len(X_test),
n_error_outliers, len(X_outliers)))

ax.legend([a.collections[0], b1, b2, c],
["learned frontier", "training observations",
"new regular observations", "new abnormal observations"],
loc="lower right",
prop=matplotlib.font_manager.FontProperties(size=10))
ax.set_title(
"%s %s (%d/%d, %d/%d, %d/%d)"
% (
model_name,
kernel_name,
n_error_train,
len(X_train),
n_error_test,
len(X_test),
n_error_outliers,
len(X_outliers),
)
)

ax.legend(
[a.collections[0], b1, b2, c],
[
"learned frontier",
"training observations",
"new regular observations",
"new abnormal observations",
],
loc="lower right",
prop=matplotlib.font_manager.FontProperties(size=10),
)

plt.show()
15 changes: 11 additions & 4 deletions sklearn/svm/_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1786,7 +1786,7 @@ class SVDD(OutlierMixin, BaseLibSVM):
Read more in the :ref:`User Guide <svm_outlier_detection>`.
..versionadded: 1.0
..versionadded: 1.1
Parameters
----------
Expand Down Expand Up @@ -1861,7 +1861,9 @@ class SVDD(OutlierMixin, BaseLibSVM):
n_features_in_ : int
Number of features seen during :term:`fit`.
.. versionadded:: 0.24
feature_names_in_ : ndarray of shape (`n_features_in_`,)
Names of features seen during :term:`fit`. Defined only when `X`
has feature names that are all strings.
n_support_ : ndarray of shape (n_classes,), dtype=int32
Number of support vectors for each class.
Expand Down Expand Up @@ -1950,8 +1952,8 @@ def fit(self, X, y=None, sample_weight=None, **params):
Parameters
----------
X : {array-like, sparse matrix} of shape (n_samples, n_features)
Set of samples, where n_samples is the number of samples and
n_features is the number of features.
Set of samples, where `n_samples` is the number of samples and
`n_features` is the number of features.
y : Ignored
Not used, present for API consistency by convention.
Expand All @@ -1963,6 +1965,11 @@ def fit(self, X, y=None, sample_weight=None, **params):
**params : dict
Additional fit parameters.
.. deprecated:: 1.0
The `fit` method will not longer accept extra keyword
parameters in 1.2. These keyword parameters were
already discarded.
Returns
-------
self : object
Expand Down

0 comments on commit 9ea9069

Please sign in to comment.