本小结是通过随机创建40个点,构造超平面,使用svm处理
1.数据集处理
随机创40个点,前20个标记为10,后20个标记为1
X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
Y = [0] * 20 + [1] * 20
打印数据集X与Y
[[-0.23594765 -1.59984279]
[-1.02126202 0.2408932 ]
[-0.13244201 -2.97727788]
[-1.04991158 -2.15135721]
[-2.10321885 -1.5894015 ]
[-1.85595643 -0.54572649]
[-1.23896227 -1.87832498]
[-1.55613677 -1.66632567]
[-0.50592093 -2.20515826]
[-1.6869323 -2.85409574]
[-4.55298982 -1.3463814 ]
[-1.1355638 -2.74216502]
[ 0.26975462 -3.45436567]
[-1.95424148 -2.18718385]
[-0.46722079 -0.53064123]
[-1.84505257 -1.62183748]
[-2.88778575 -3.98079647]
[-2.34791215 -1.84365103]
[-0.76970932 -0.79762015]
[-2.38732682 -2.30230275]
[ 0.95144703 0.57998206]
[ 0.29372981 3.9507754 ]
[ 1.49034782 1.5619257 ]
[ 0.74720464 2.77749036]
[ 0.38610215 1.78725972]
[ 1.10453344 2.3869025 ]
[ 1.48919486 0.81936782]
[ 1.97181777 2.42833187]
[ 2.06651722 2.3024719 ]
[ 1.36567791 1.63725883]
[ 1.32753955 1.64044684]
[ 1.18685372 0.2737174 ]
[ 2.17742614 1.59821906]
[ 0.36980165 2.46278226]
[ 1.09270164 2.0519454 ]
[ 2.72909056 2.12898291]
[ 3.13940068 0.76517418]
[ 2.40234164 1.31518991]
[ 1.12920285 1.42115034]
[ 1.68844747 2.05616534]]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
2.训练数据集
构造超平面,使用svm处理
# fit the model
clf = svm.SVC(kernel='linear')
clf.fit(X, Y)
# get the separating hyperplane
w = clf.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (clf.intercept_[0]) / w[1]
# plot the parallels to the separating hyperplane that pass through the
# support vectors
b = clf.support_vectors_[0]
yy_down = a * xx + (b[1] - a * b[0])
b = clf.support_vectors_[-1]
yy_up = a * xx + (b[1] - a * b[0])
3.完整代码
源码如下
print(__doc__)
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm
# we create 40 separable points
np.random.seed(0)
X = np.r_[np.random.randn(20, 2) - [2, 2], np.random.randn(20, 2) + [2, 2]]
Y = [0] * 20 + [1] * 20
# fit the model
clf = svm.SVC(kernel='linear')
clf.fit(X, Y)
# get the separating hyperplane
w = clf.coef_[0]
a = -w[0] / w[1]
xx = np.linspace(-5, 5)
yy = a * xx - (clf.intercept_[0]) / w[1]
# plot the parallels to the separating hyperplane that pass through the
# support vectors
b = clf.support_vectors_[0]
yy_down = a * xx + (b[1] - a * b[0])
b = clf.support_vectors_[-1]
yy_up = a * xx + (b[1] - a * b[0])
# plot the line, the points, and the nearest vectors to the plane
plt.plot(xx, yy, 'k-')
plt.plot(xx, yy_down, 'k--')
plt.plot(xx, yy_up, 'k--')
plt.scatter(clf.support_vectors_[:, 0], clf.support_vectors_[:, 1],
s=80, facecolors='none')
plt.scatter(X[:, 0], X[:, 1], c=Y, cmap=plt.cm.Paired)
plt.axis('tight')
plt.show()
4.运行结果