1. 背景:

     1.1 最早是由 Vladimir N. Vapnik 和 Alexey Ya. Chervonenkis 在1963年提出

     1.2 目前的版本(soft margin)是由Corinna Cortes 和 Vapnik在1993年提出,并在1995年发表

     1.3 深度学习(2012)出现之前,SVM被认为机器学习中近十几年来最成功,表现最好的算法

2. 机器学习的一般框架:

     训练集 => 提取特征向量 => 结合一定的算法(分类器:比如决策树,KNN)=>得到结果

 例子

QQ截图20170612172546

 

两类?哪条线最好?

  3.2 SVM寻找区分两类的超平面(hyper plane), 使边际(margin)最大

QQ截图20170612172633

总共可以有多少个可能的超平面?无数条

               如何选取使边际(margin)最大的超平面 (Max Margin Hyperplane)?

               超平面到一侧最近点的距离等于到另一侧最近点的距离,两侧的两个超平面平行

3. 线性可区分(linear separable) 和 线性不可区分 (linear inseparable)

QQ截图20170612172728

4. 定义与公式建立

QQ截图20170612172802

QQ截图20170612172835

QQ截图20170612172907

QQ截图20170612172944

QQ截图20170612173026

QQ截图20170612173130

 

QQ截图20170612173202

QQ截图20170612173211

 

例子

QQ截图20170612173249

使用python实现的示例

from sklearn import svm

x = [[2, 0], [1, 1], [2, 3]]
y = [0, 0, 1]
clf = svm.SVC(kernel = 'linear')
clf.fit(x, y)

print clf

# 得到支持向量 get support vectors
print clf.support_vectors_
# 得到支持向量指数
print clf.support_
# 获取每个类的支持向量数
print clf.n_support_

#进行预测 得出 0 也就是线下方
print clf.predict([2,0])

QQ截图20170612173259

 

例子2:模拟画线支持向量机(线性可区分)的示例代码

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.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])

print "w: ", w
print "a: ", a

# print "xx: ", xx
# print "yy: ", yy
print "support_vectors_: ", clf.support_vectors_
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.support_vectors_[:, 0], clf.support_vectors_[:, 1],
          s=80, facecolors='none')
pl.scatter(X[:, 0], X[:, 1], c=Y, cmap=pl.cm.Paired)

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

输出图像

20170613140447

下载svm示例代码