Plot Separating HyperplaneΒΆ

========================================= SVM: Maximum margin separating hyperplaneΒΆ

Plot the maximum margin separating hyperplane within a two-class separable dataset using a Support Vector Machine classifier with linear kernel.

Imports for Maximum-Margin Separating Hyperplane with SVMΒΆ

The maximum-margin hyperplane is the central concept behind Support Vector Machines. Given linearly separable data, infinitely many hyperplanes can separate the two classes, but the SVM selects the one that maximizes the margin – the perpendicular distance to the closest points of either class. This choice is motivated by statistical learning theory, which shows that wider margins lead to better generalization bounds.

Visualizing the geometry: The solid line is the decision boundary, the dashed lines are the margin boundaries, and the circled points are the support vectors – the training samples that lie exactly on the margin. Setting C=1000 (very high) creates a hard-margin SVM that tolerates no violations, clearly showing the maximum-margin geometry. DecisionBoundaryDisplay renders the contour levels of the decision function at -1, 0, and +1, corresponding to the negative margin, decision boundary, and positive margin respectively.

# Authors: The scikit-learn developers
# SPDX-License-Identifier: BSD-3-Clause

import matplotlib.pyplot as plt

from sklearn import svm
from sklearn.datasets import make_blobs
from sklearn.inspection import DecisionBoundaryDisplay

# we create 40 separable points
X, y = make_blobs(n_samples=40, centers=2, random_state=6)

# fit the model, don't regularize for illustration purposes
clf = svm.SVC(kernel="linear", C=1000)
clf.fit(X, y)

plt.scatter(X[:, 0], X[:, 1], c=y, s=30, cmap=plt.cm.Paired)

# plot the decision function
ax = plt.gca()
DecisionBoundaryDisplay.from_estimator(
    clf,
    X,
    plot_method="contour",
    colors="k",
    levels=[-1, 0, 1],
    alpha=0.5,
    linestyles=["--", "-", "--"],
    ax=ax,
)
# plot support vectors
ax.scatter(
    clf.support_vectors_[:, 0],
    clf.support_vectors_[:, 1],
    s=100,
    linewidth=1,
    facecolors="none",
    edgecolors="k",
)
plt.show()