# SVM

1 由来

2 最大间隔分类器

3 核函数

4 松弛变量

5程序代码：
import numpy as np
import pylab as pl
from sklearn import svm

#we create 40 separable points
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.supportvectors[0]
yy_down = axx + (b[1] - ab[0])
b = clf.supportvectors[-1]
yy_up = axx + (b[1] - ab[0])

print("w: ", w)
print("a: ", a)

#print "xx: ", xx
#print "yy: ", yy
print("supportvectors: ", clf.supportvectors)
print("clf.coef: ", clf.coef)

#switching to the generic n-dimensional parameterization of the hyperplan to the 2D-specific equation
#of a line y=a.x +b: the generic w_0x + w_1y +w_3=0 can be rewritten y = -(w_0/w_1) x + (w_3/w_1)

#plot the line, the points, and the nearest vectors to the plane
pl.plot(xx, yy, 'k-')
pl.plot(xx, yy_down, 'k--')
pl.plot(xx, yy_up, 'k--')

pl.scatter(clf.supportvectors[:, 0], clf.supportvectors[:, 1],
s=80, facecolors='none')
pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)

pl.axis('tight')
pl.show()