Skip to content

Commit ffbe4ab

Browse files
adrinjalalijeremiedbb
authored andcommitted
DOC remove obsolete SVM example (#27108)
1 parent 4647729 commit ffbe4ab

File tree

3 files changed

+46
-59
lines changed

3 files changed

+46
-59
lines changed

doc/conf.py

+1
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@
301301
"auto_examples/decomposition/plot_beta_divergence": (
302302
"auto_examples/applications/plot_topics_extraction_with_nmf_lda"
303303
),
304+
"auto_examples/svm/plot_svm_nonlinear": "auto_examples/svm/plot_svm_kernels",
304305
"auto_examples/ensemble/plot_adaboost_hastie_10_2": (
305306
"auto_examples/ensemble/plot_adaboost_multiclass"
306307
),

examples/svm/plot_svm_kernels.py

+45-14
Original file line numberDiff line numberDiff line change
@@ -110,12 +110,15 @@
110110
from sklearn.inspection import DecisionBoundaryDisplay
111111

112112

113-
def plot_training_data_with_decision_boundary(kernel):
113+
def plot_training_data_with_decision_boundary(
114+
kernel, ax=None, long_title=True, support_vectors=True
115+
):
114116
# Train the SVC
115117
clf = svm.SVC(kernel=kernel, gamma=2).fit(X, y)
116118

117119
# Settings for plotting
118-
_, ax = plt.subplots(figsize=(4, 3))
120+
if ax is None:
121+
_, ax = plt.subplots(figsize=(4, 3))
119122
x_min, x_max, y_min, y_max = -3, 3, -3, 3
120123
ax.set(xlim=(x_min, x_max), ylim=(y_min, y_max))
121124

@@ -136,20 +139,26 @@ def plot_training_data_with_decision_boundary(kernel):
136139
linestyles=["--", "-", "--"],
137140
)
138141

139-
# Plot bigger circles around samples that serve as support vectors
140-
ax.scatter(
141-
clf.support_vectors_[:, 0],
142-
clf.support_vectors_[:, 1],
143-
s=250,
144-
facecolors="none",
145-
edgecolors="k",
146-
)
142+
if support_vectors:
143+
# Plot bigger circles around samples that serve as support vectors
144+
ax.scatter(
145+
clf.support_vectors_[:, 0],
146+
clf.support_vectors_[:, 1],
147+
s=150,
148+
facecolors="none",
149+
edgecolors="k",
150+
)
151+
147152
# Plot samples by color and add legend
148-
ax.scatter(X[:, 0], X[:, 1], c=y, s=150, edgecolors="k")
153+
ax.scatter(X[:, 0], X[:, 1], c=y, s=30, edgecolors="k")
149154
ax.legend(*scatter.legend_elements(), loc="upper right", title="Classes")
150-
ax.set_title(f" Decision boundaries of {kernel} kernel in SVC")
155+
if long_title:
156+
ax.set_title(f" Decision boundaries of {kernel} kernel in SVC")
157+
else:
158+
ax.set_title(kernel)
151159

152-
_ = plt.show()
160+
if ax is None:
161+
plt.show()
153162

154163

155164
# %%
@@ -237,7 +246,6 @@ def plot_training_data_with_decision_boundary(kernel):
237246
# using the hyperbolic tangent function (:math:`\tanh`). The kernel function
238247
# scales and possibly shifts the dot product of the two points
239248
# (:math:`\mathbf{x}_1` and :math:`\mathbf{x}_2`).
240-
241249
plot_training_data_with_decision_boundary("sigmoid")
242250

243251
# %%
@@ -271,3 +279,26 @@ def plot_training_data_with_decision_boundary(kernel):
271279
# parameters using techniques such as
272280
# :class:`~sklearn.model_selection.GridSearchCV` is recommended to capture the
273281
# underlying structures within the data.
282+
283+
# %%
284+
# XOR dataset
285+
# -----------
286+
# A classical example of a dataset which is not linearly separable is the XOR
287+
# pattern. HEre we demonstrate how different kernels work on such a dataset.
288+
289+
xx, yy = np.meshgrid(np.linspace(-3, 3, 500), np.linspace(-3, 3, 500))
290+
np.random.seed(0)
291+
X = np.random.randn(300, 2)
292+
y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)
293+
294+
_, ax = plt.subplots(2, 2, figsize=(8, 8))
295+
args = dict(long_title=False, support_vectors=False)
296+
plot_training_data_with_decision_boundary("linear", ax[0, 0], **args)
297+
plot_training_data_with_decision_boundary("poly", ax[0, 1], **args)
298+
plot_training_data_with_decision_boundary("rbf", ax[1, 0], **args)
299+
plot_training_data_with_decision_boundary("sigmoid", ax[1, 1], **args)
300+
plt.show()
301+
302+
# %%
303+
# As you can see from the plots above, only the `rbf` kernel can find a
304+
# reasonable decision boundary for the above dataset.

examples/svm/plot_svm_nonlinear.py

-45
This file was deleted.

0 commit comments

Comments
 (0)