2010-04-23 13:52:16 +08:00
|
|
|
"""
|
2010-11-02 18:38:06 +08:00
|
|
|
=========================================
|
2010-10-19 21:16:27 +08:00
|
|
|
SVM: Maximum margin separating hyperplane
|
2010-11-02 18:38:06 +08:00
|
|
|
=========================================
|
2010-04-23 13:52:16 +08:00
|
|
|
|
2010-10-19 21:16:27 +08:00
|
|
|
Plot the maximum margin separating hyperplane within a two-class
|
2015-07-15 09:37:08 +08:00
|
|
|
separable dataset using a Support Vector Machine classifier with
|
2010-10-19 21:16:27 +08:00
|
|
|
linear kernel.
|
2021-10-22 21:33:22 +08:00
|
|
|
|
2010-04-23 13:52:16 +08:00
|
|
|
"""
|
|
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
import matplotlib.pyplot as plt
|
2011-09-02 17:00:02 +08:00
|
|
|
from sklearn import svm
|
2017-08-02 03:11:48 +08:00
|
|
|
from sklearn.datasets import make_blobs
|
2022-03-29 22:36:31 +08:00
|
|
|
from sklearn.inspection import DecisionBoundaryDisplay
|
2017-08-02 03:11:48 +08:00
|
|
|
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
# we create 40 separable points
|
2017-08-06 11:28:08 +08:00
|
|
|
X, y = make_blobs(n_samples=40, centers=2, random_state=6)
|
2010-04-23 13:52:16 +08:00
|
|
|
|
2017-08-06 11:28:08 +08:00
|
|
|
# fit the model, don't regularize for illustration purposes
|
|
|
|
|
clf = svm.SVC(kernel="linear", C=1000)
|
2017-08-02 03:11:48 +08:00
|
|
|
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()
|
2022-03-29 22:36:31 +08:00
|
|
|
DecisionBoundaryDisplay.from_estimator(
|
|
|
|
|
clf,
|
|
|
|
|
X,
|
|
|
|
|
plot_method="contour",
|
|
|
|
|
colors="k",
|
|
|
|
|
levels=[-1, 0, 1],
|
|
|
|
|
alpha=0.5,
|
|
|
|
|
linestyles=["--", "-", "--"],
|
|
|
|
|
ax=ax,
|
2017-08-02 03:11:48 +08:00
|
|
|
)
|
|
|
|
|
# plot support vectors
|
|
|
|
|
ax.scatter(
|
|
|
|
|
clf.support_vectors_[:, 0],
|
|
|
|
|
clf.support_vectors_[:, 1],
|
|
|
|
|
s=100,
|
2018-06-11 13:22:48 +08:00
|
|
|
linewidth=1,
|
|
|
|
|
facecolors="none",
|
|
|
|
|
edgecolors="k",
|
|
|
|
|
)
|
2017-08-06 11:28:08 +08:00
|
|
|
plt.show()
|