Skip to content

Commit 1550432

Browse files
DOC fix deprecation warning in plot_oneclass (scikit-learn#27452)
Co-authored-by: Arturo Amor <86408019+ArturoAmorQ@users.noreply.github.com>
1 parent 8ad8593 commit 1550432

File tree

1 file changed

+51
-20
lines changed

1 file changed

+51
-20
lines changed

examples/svm/plot_oneclass.py

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@
1111
1212
"""
1313

14-
import matplotlib.font_manager
15-
import matplotlib.pyplot as plt
14+
# %%
1615
import numpy as np
1716

1817
from sklearn import svm
1918

20-
xx, yy = np.meshgrid(np.linspace(-5, 5, 500), np.linspace(-5, 5, 500))
2119
# Generate train data
2220
X = 0.3 * np.random.randn(100, 2)
2321
X_train = np.r_[X + 2, X - 2]
@@ -37,24 +35,52 @@
3735
n_error_test = y_pred_test[y_pred_test == -1].size
3836
n_error_outliers = y_pred_outliers[y_pred_outliers == 1].size
3937

40-
# plot the line, the points, and the nearest vectors to the plane
41-
Z = clf.decision_function(np.c_[xx.ravel(), yy.ravel()])
42-
Z = Z.reshape(xx.shape)
38+
# %%
39+
import matplotlib.font_manager
40+
import matplotlib.lines as mlines
41+
import matplotlib.pyplot as plt
42+
43+
from sklearn.inspection import DecisionBoundaryDisplay
4344

44-
plt.title("Novelty Detection")
45-
plt.contourf(xx, yy, Z, levels=np.linspace(Z.min(), 0, 7), cmap=plt.cm.PuBu)
46-
a = plt.contour(xx, yy, Z, levels=[0], linewidths=2, colors="darkred")
47-
plt.contourf(xx, yy, Z, levels=[0, Z.max()], colors="palevioletred")
45+
_, ax = plt.subplots()
46+
47+
# generate grid for the boundary display
48+
xx, yy = np.meshgrid(np.linspace(-5, 5, 10), np.linspace(-5, 5, 10))
49+
X = np.concatenate([xx.reshape(-1, 1), yy.reshape(-1, 1)], axis=1)
50+
DecisionBoundaryDisplay.from_estimator(
51+
clf,
52+
X,
53+
response_method="decision_function",
54+
plot_method="contourf",
55+
ax=ax,
56+
cmap="PuBu",
57+
)
58+
DecisionBoundaryDisplay.from_estimator(
59+
clf,
60+
X,
61+
response_method="decision_function",
62+
plot_method="contourf",
63+
ax=ax,
64+
levels=[0, 10000],
65+
colors="palevioletred",
66+
)
67+
DecisionBoundaryDisplay.from_estimator(
68+
clf,
69+
X,
70+
response_method="decision_function",
71+
plot_method="contour",
72+
ax=ax,
73+
levels=[0],
74+
colors="darkred",
75+
linewidths=2,
76+
)
4877

4978
s = 40
50-
b1 = plt.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k")
51-
b2 = plt.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k")
52-
c = plt.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k")
53-
plt.axis("tight")
54-
plt.xlim((-5, 5))
55-
plt.ylim((-5, 5))
79+
b1 = ax.scatter(X_train[:, 0], X_train[:, 1], c="white", s=s, edgecolors="k")
80+
b2 = ax.scatter(X_test[:, 0], X_test[:, 1], c="blueviolet", s=s, edgecolors="k")
81+
c = ax.scatter(X_outliers[:, 0], X_outliers[:, 1], c="gold", s=s, edgecolors="k")
5682
plt.legend(
57-
[a.collections[0], b1, b2, c],
83+
[mlines.Line2D([], [], color="darkred"), b1, b2, c],
5884
[
5985
"learned frontier",
6086
"training observations",
@@ -64,8 +90,13 @@
6490
loc="upper left",
6591
prop=matplotlib.font_manager.FontProperties(size=11),
6692
)
67-
plt.xlabel(
68-
"error train: %d/200 ; errors novel regular: %d/40 ; errors novel abnormal: %d/40"
69-
% (n_error_train, n_error_test, n_error_outliers)
93+
ax.set(
94+
xlabel=(
95+
f"error train: {n_error_train}/200 ; errors novel regular: {n_error_test}/40 ;"
96+
f" errors novel abnormal: {n_error_outliers}/40"
97+
),
98+
title="Novelty Detection",
99+
xlim=(-5, 5),
100+
ylim=(-5, 5),
70101
)
71102
plt.show()

0 commit comments

Comments
 (0)