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.
|
2010-04-23 13:52:16 +08:00
|
|
|
"""
|
2013-02-01 22:04:03 +08:00
|
|
|
print(__doc__)
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
import numpy as np
|
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
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
# we create 40 separable points
|
|
|
|
|
np.random.seed(0)
|
2011-12-17 05:55:42 +08:00
|
|
|
X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
|
|
|
|
|
Y = [0] * 20 + [1] * 20
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
# fit the model
|
2012-05-06 03:02:28 +08:00
|
|
|
clf = svm.SVC(kernel='linear')
|
2010-04-23 13:52:16 +08:00
|
|
|
clf.fit(X, Y)
|
|
|
|
|
|
|
|
|
|
# get the separating hyperplane
|
2011-12-17 05:55:42 +08:00
|
|
|
w = clf.coef_[0]
|
|
|
|
|
a = -w[0] / w[1]
|
2010-04-23 13:52:16 +08:00
|
|
|
xx = np.linspace(-5, 5)
|
2011-12-17 05:55:42 +08:00
|
|
|
yy = a * xx - (clf.intercept_[0]) / w[1]
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
# plot the parallels to the separating hyperplane that pass through the
|
|
|
|
|
# support vectors
|
2010-10-19 21:16:27 +08:00
|
|
|
b = clf.support_vectors_[0]
|
2011-12-17 05:55:42 +08:00
|
|
|
yy_down = a * xx + (b[1] - a * b[0])
|
2010-10-19 21:16:27 +08:00
|
|
|
b = clf.support_vectors_[-1]
|
2011-12-17 05:55:42 +08:00
|
|
|
yy_up = a * xx + (b[1] - a * b[0])
|
2010-04-23 13:52:16 +08:00
|
|
|
|
|
|
|
|
# plot the line, the points, and the nearest vectors to the plane
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.plot(xx, yy, 'k-')
|
|
|
|
|
plt.plot(xx, yy_down, 'k--')
|
|
|
|
|
plt.plot(xx, yy_up, 'k--')
|
2011-02-02 18:51:16 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
|
2014-05-15 10:35:13 +08:00
|
|
|
s=80, facecolors='none')
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
|
2010-04-23 13:52:16 +08:00
|
|
|
|
2014-05-15 04:31:03 +08:00
|
|
|
plt.axis('tight')
|
|
|
|
|
plt.show()
|