SVM深入理解【人工智能】

一、SVM算法

  1. 支持向量机(Support Vector Machine,常简称为SVM)是一种监督式学习的方法,可广泛地应用于统计分类以及回归分析。
  2. 它是将向量映射到一个更高维的空间里,在这个空间里建立有一个最大间隔超平面。在分开数据的超平面的两边建有两个互相平行的超平面,分隔超平面使两个平行超平面的距离最大化。假定平行超平面间的距离或差距越大,分类器的总误差越小。

1.核方法

在线性 SVM 中转化为最优化问题时求解的公式计算都是以内积(dot product)形式出现的,其中 ϕ ( X ) \phi(X)ϕ(X ) 是把训练集中的向量点转化到高维的非线性映射函数,因为内积的算法复杂度非常大,所以我们利用核函数来取代计算非线性映射函数的内积。

以下核函数和非线性映射函数的内积等同,但核函数 K 的运算量要远少于求内积。
K ( X i , X j ) = ϕ ( X i ) ⋅ ϕ ( X j ) K(X_i,X_j)=\phi(X_i)·\phi(X_j)K (X i ​,X j ​)=ϕ(X i ​)⋅ϕ(X j ​)

(1)常用的核函数

h 度多项式核函数(polynomial kernel of degree h):
K ( X i , X j ) = ( X i , X j + 1 ) h K(X_i,X_j)=(X_i,X_j+1)^h K (X i ​,X j ​)=(X i ​,X j ​+1 )h

高斯径向基核函数(Gaussian radial basis function kernel):
K ( X i , X j ) = e − ∣ ∣ X i − X j ∣ ∣ 2 / 2 σ 2 K(X_i,X_j)=e{-||X_i-X_j||2/2\sigma^2}K (X i ​,X j ​)=e −∣∣X i ​−X j ​∣∣2 /2 σ2

S 型核函数(Sigmoid function kernel):
K ( X i , X j ) = t a n h ( k X i ⋅ X j − δ ) K(X_i,X_j)=tanh(kX_i·X_j-\delta)K (X i ​,X j ​)=t a n h (k X i ​⋅X j ​−δ)

如何选择使用哪个 kernel ?

  1. 根据先验知识,比如图像分类,通常使用 RBF(高斯径向基核函数),文字不使用 RBF。
  2. 尝试不同的 kernel,根据结果准确度而定尝试不同的 kernel,根据结果准确度而定。

(2)核函数举例

假设定义两个向量:x = ( x 1 , x 2 , x 3 ) x = (x_1, x_2, x_3)x =(x 1 ​,x 2 ​,x 3 ​),y = ( y 1 , y 2 , y 3 ) y = (y_1, y_2, y_3)y =(y 1 ​,y 2 ​,y 3 ​)

定义方程:
f ( x ) = ( x 1 x 1 , x 1 x 2 , x 1 x 3 , x 2 x 1 , x 2 x 2 , x 2 x 3 , x 3 x 1 , x 3 x 2 , x 3 x 3 ) f(x) = (x_1x_1, x_1x_2, x_1x_3, x_2x_1, x_2x_2, x_2x_3, x_3x_1, x_3x_2, x_3x_3)f (x )=(x 1 ​x 1 ​,x 1 ​x 2 ​,x 1 ​x 3 ​,x 2 ​x 1 ​,x 2 ​x 2 ​,x 2 ​x 3 ​,x 3 ​x 1 ​,x 3 ​x 2 ​,x 3 ​x 3 ​)

核函数:K ( x , y ) = ( < x , y > ) 2 K(x,y ) = (K (x ,y )=(<x ,y >)2

假设:x = ( 1 , 2 , 3 ) x = (1, 2, 3)x =(1 ,2 ,3 ),y = ( 4 , 5 , 6 ) y = (4, 5, 6)y =(4 ,5 ,6 )

不用核函数,直接求内积:
f ( x ) = ( 1 , 2 , 3 , 2 , 4 , 6 , 3 , 6 , 9 ) f(x) = (1, 2, 3, 2, 4, 6, 3, 6, 9)f (x )=(1 ,2 ,3 ,2 ,4 ,6 ,3 ,6 ,9 )f ( y ) = ( 16 , 20 , 24 , 20 , 25 , 36 , 24 , 30 , 36 ) f(y) = (16, 20, 24, 20, 25, 36, 24, 30, 36)f (y )=(1 6 ,2 0 ,2 4 ,2 0 ,2 5 ,3 6 ,2 4 ,3 0 ,3 6 )< f ( x ) , f ( y ) > = 16 + 40 + 72 + 40 + 100 + 180 + 72 + 180 + 324 = 1024 <f (x ),f (y )>=1 6 +4 0 +7 2 +4 0 +1 0 0 +1 8 0 +7 2 +1 8 0 +3 2 4 =1 0 2 4

使用核函数:
K ( x , y ) = ( 4 + 10 + 18 ) 2 = 3 2 2 = 1024 K(x, y) = (4 + 10 + 18 ) ^2 = 32^2 = 1024 K (x ,y )=(4 +1 0 +1 8 )2 =3 2 2 =1 0 2 4

同样的结果,使用 kernel 方法计算容易很多。而这只是 9 维的情况,如果维度更高,那么直接求内积的方法运算复杂度会非常大。

所以使用 kernel 的意义在于:

  1. 将向量的维度从低维映射到高维
  2. 降低运算复杂度降低运算复杂度

二、 月亮数据集

x_moom, y_moom = datasets.make_moons()

plt.scatter(x_moom[y_moom==0,0],x_moom[y_moom==0,1])
plt.scatter(x_moom[y_moom==1,0],x_moom[y_moom==1,1])
plt.show()

在这里插入图片描述

x_moom, y_moom = datasets.make_moons(noise=0.15,random_state=520)

plt.scatter(x_moom[y_moom==0,0],x_moom[y_moom==0,1])
plt.scatter(x_moom[y_moom==1,0],x_moom[y_moom==1,1])
plt.show()

在这里插入图片描述

  • 多项式拟合
poly_svc_moom = PolynomialSVC(degree=5,C=10)

poly_svc_moom.fit(x_moom,y_moom)

print("权重w:",poly_svc_moom.named_steps['linearSVC'].coef_[0])
print("截距b:",poly_svc_moom.named_steps['linearSVC'].intercept_[0])

plot_decision_boundary(poly_svc_moom,axis=[-1.5,2.5,-1.5,2.5])

plt.scatter(x_moom[y_moom==0,0],x_moom[y_moom==0,1])
plt.scatter(x_moom[y_moom==1,0],x_moom[y_moom==1,1])
plt.show()

在这里插入图片描述

在这里插入图片描述

  • 高斯拟合
rbf_svc_moom = RBFKernelSVC(1)

rbf_svc_moom.fit(x_moom,y_moom)

print("系数w:",rbf_svc_moom.named_steps['svc'].dual_coef_)
print("截距b:",rbf_svc_moom.named_steps['svc'].intercept_)

plot_decision_boundary(rbf_svc_moom,axis=[-1.5,2.5,-1.0,1.5])

plt.scatter(x_moom[y_moom==0,0],x_moom[y_moom==0,1])
plt.scatter(x_moom[y_moom==1,0],x_moom[y_moom==1,1])
plt.show()

在这里插入图片描述

三、 鸢尾花数据集

iris = datasets.load_iris()

x_iris = iris.data
y_iris = iris.target

x_iris = x_iris [y_iris<2,:2]
y_iris = y_iris[y_iris<2]

plt.scatter(x_iris[y_iris==0,0],x_iris[y_iris==0,1])
plt.scatter(x_iris[y_iris==1,0],x_iris[y_iris==1,1])
plt.show()

在这里插入图片描述

  • 多项式拟合
poly_svc_iris = PolynomialSVC(degree=5,C=10)

poly_svc_iris.fit(x_iris,y_iris)

print("权重w:",poly_svc_iris.named_steps['linearSVC'].coef_[0])
print("截距b:",poly_svc_iris.named_steps['linearSVC'].intercept_[0])

plot_decision_boundary(poly_svc_iris,axis=[4,7.5,1,4.5])

plt.scatter(x_iris[y_iris==0,0],x_iris[y_iris==0,1])
plt.scatter(x_iris[y_iris==1,0],x_iris[y_iris==1,1])
plt.show()

在这里插入图片描述

  • 高斯拟合
rbf_svc_iris = RBFKernelSVC(1)

rbf_svc_iris.fit(x_iris,y_iris)

print("系数w:",rbf_svc_iris.named_steps['svc'].dual_coef_)
print("截距b:",rbf_svc_iris.named_steps['svc'].intercept_)

plot_decision_boundary(rbf_svc_iris,axis=[4,7.5,1,4.5])

plt.scatter(x_iris[y_iris==0,0],x_iris[y_iris==0,1])
plt.scatter(x_iris[y_iris==1,0],x_iris[y_iris==1,1])
plt.show()

在这里插入图片描述

四、例子重做

import numpy as np
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.preprocessing import StandardScaler
from sklearn.svm import LinearSVC
iris = datasets.load_iris()

X = iris.data
y = iris.target

X = X [y<2,:2]
y = y[y<2]

plt.scatter(X[y==0,0],X[y==0,1],color='red')
plt.scatter(X[y==1,0],X[y==1,1],color='blue')
plt.show()

在这里插入图片描述

  1. 高斯核
import numpy as np
import matplotlib.pyplot as plt

x = np.arange(-4,5,1)
y = np.array((x >= -2 ) & (x  2),dtype='int')

plt.scatter(x[y==0],[0]*len(x[y==0]))
plt.scatter(x[y==1],[0]*len(x[y==1]))
plt.show()

在这里插入图片描述

def gaussian(x,l):
    gamma = 1.0
    return np.exp(-gamma * (x -l)**2)
l1,l2 = -1,1
X_new = np.empty((len(x),2))
for i,data in enumerate(x):
    X_new[i,0] = gaussian(data,l1)
    X_new[i,1] = gaussian(data,l2)
plt.scatter(X_new[y==0,0],X_new[y==0,1])
plt.scatter(X_new[y==1,0],X_new[y==1,1])
plt.show()

在这里插入图片描述

四、参考文献

1.【机器学习】机器学习之支持向量机(SVM)
2.线性判别准则与线性分类编程实践
3.SVM算法补充

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值