6.0 SVM支持向量机
使用支持向量机(SVM)处理各种两维的样本数据集,了解支持向量机如何工作,以及如何使用带高斯核函数的SVM。
1.导入数据并绘制第一个数据集的图像
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/607e7cb08e5d6a9a7d3c9566dadef76f.png)
data1=sio.loadmat('ex6data1.mat')
data2=sio.loadmat('ex6data2.mat')
data3=sio.loadmat('ex6data3.mat')
print(data1.keys())
print(data2.keys())
print(data3.keys())
X1,y1=data1['X'],data1['y'].flatten()
X2,y2=data2['X'],data2['y'].flatten()
X3,y3=data3['X'],data3['y'].flatten()
Xval,yval=data3['Xval'],data3['yval'].flatten()
print(X1.shape,y1.shape)
print(X2.shape,y2.shape)
print(X3.shape,y3.shape)
print(Xval.shape,yval.shape)
def plot_data(x,y):
n=x[y==0]
p=x[y==1]
plt.scatter(p[:,0],p[:,1],c='r',marker='x',label='y=1')
plt.scatter(n[:,0],n[:,1],c='g',marker='o',edgecolors='g',linewidths=0.5,label='y=0')
plt.legend()
plt.show()
plot_data(X1,y1)
2.使用sklearn.svm拟合并预测
关于sklearn.svm模块,参考了该佬的总结
from sklearn.svm import SVC
svc1=SVC(C=1,kernel='linear')
svc1.fit(X1,y1.flatten())
svc1.predict(X1)
print(svc1.score(X1,y1.flatten()))
3.绘制决策边界
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/a61a7ecc2bd3c0513adedca764816dff.png)
def plot_boundary(svc,X1):
u=np.linspace(np.min(X1[:,0]),np.max(X1[:,0]),500)
v=np.linspace(np.min(X1[:,1]),np.max(X1[:,1]),500)
x,y=np.meshgrid(u,v)
z=svc.predict(np.c_[x.flatten(),y.flatten()])
z=z.reshape(x.shape)
plt.contour(x,y,z,1,colors='b')
plt.title('The Decision Boundary')
plt.show()
plt.figure(1)
plot_data(X1,y1)
plot_boundary(svc1,X1)
plt.show()
4.绘制第二个数据集的决策边界(使用内置高斯核函数)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e1243cffcd1dbcdeba6e53d7edf0dc8a.png)
svc2=SVC(C=100,kernel='rbf',gamma=np.power(0.1,-2)/2)
svc2.fit(X2,y2)
svc1.predict([[0.4,0.9]])
print(svc2.score(X2,y2.flatten()))
plt.figure(2)
plot_data(X2,y2)
plot_boundary(svc2,X2)
plt.show
5.在验证集上寻找最佳参数,并绘制第三个数据集的决策边界
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/e6d7c1ea4edfc2a0e0cb624b4d3f06bd.png)
gammas = [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100]
Cvalues = [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100]
best_score = 0
best_pramas = (0, 0)
for c in Cvalues:
for gamma in gammas:
svc3 = SVC(C=c, kernel='rbf')
svc3.fit(X3, y3)
score = svc3.score(Xval, yval)
if score > best_score:
best_score = score
best_pramas = (c, gamma)
print(best_score, best_pramas)
plt.figure(3)
plot_data(X3, y3)
plot_boundary(svc3, X3)
plt.show
完整代码
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import scipy.io as sio
from scipy.optimize import minimize
from sklearn.svm import SVC
data1=sio.loadmat('ex6data1.mat')
data2=sio.loadmat('ex6data2.mat')
data3=sio.loadmat('ex6data3.mat')
X1,y1=data1['X'],data1['y'].flatten()
X2,y2=data2['X'],data2['y'].flatten()
X3,y3=data3['X'],data3['y'].flatten()
Xval,yval=data3['Xval'],data3['yval'].flatten()
def plot_data(x,y):
n=x[y==0]
p=x[y==1]
plt.scatter(p[:,0],p[:,1],c='r',marker='x',label='y=1')
plt.scatter(n[:,0],n[:,1],c='g',marker='o',edgecolors='g',linewidths=0.5,label='y=0')
plt.legend()
plt.show
svc1=SVC(C=1,kernel='linear')
svc1.fit(X1,y1.flatten())
svc1.predict(X1)
def plot_boundary(svc,X1):
u=np.linspace(np.min(X1[:,0]),np.max(X1[:,0]),500)
v=np.linspace(np.min(X1[:,1]),np.max(X1[:,1]),500)
x,y=np.meshgrid(u,v)
z=svc.predict(np.c_[x.flatten(),y.flatten()])
z=z.reshape(x.shape)
plt.contour(x,y,z,1,colors='b')
plt.title('The Decision Boundary')
plt.show()
plt.figure(1)
plot_data(X1,y1)
plot_boundary(svc1,X1)
plt.show()
plot_data(X2,y2)
def gaussianKernel(x1,x2,sigma):
return np.exp(-((x1-x2).T@(x1-x2))/(2*sigma*sigma))
svc2=SVC(C=100,kernel='rbf',gamma=np.power(0.1,-2)/2)
svc2.fit(X2,y2)
svc1.predict([[0.4,0.9]])
plt.figure(2)
plot_data(X2,y2)
plot_boundary(svc2,X2)
plt.show
gammas = [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100]
Cvalues = [0.01, 0.03, 0.1, 0.3, 1, 3, 10, 30, 100]
best_score = 0
best_pramas = (0, 0)
for c in Cvalues:
for gamma in gammas:
svc3 = SVC(C=c, kernel='rbf')
svc3.fit(X3, y3)
score = svc3.score(Xval, yval)
if score > best_score:
best_score = score
best_pramas = (c, gamma)
print(best_score, best_pramas)
plt.figure(3)
plot_data(X3, y3)
plot_boundary(svc3, X3)
plt.show