1 初始准备
import numpy as np
import matplotlib.pyplot as pl
from sklearn import svm
from sklearn.datasets import make_blobs
%matplotlib inline
2随机生成数据
随机生成两个数据簇,可以保证线性可分
X,y=make_blobs(n_samples=100,centers=2,random_state=3)
X.shape,y.shape
((100, 2), (100,))
3 线性核分类
#简单线性核
clf=svm.SVC(kernel='linear',C=1000.0)
clf.fit(X,y)
pl.scatter(X[:,0],X[:,1],c=y,s=30,cmap=pl.cm.Paired)
ax=pl.gca()
xlim=ax.get_xlim()
ylim=ax.get_ylim()
#计算决策边界
xx=np.linspace(xlim[0],xlim[1],30)
yy=np.linspace(ylim[0],ylim[1],30)
YY,XX=np.meshgrid(yy,xx)
xy=np.vstack([XX.ravel(),YY.ravel()]).T
Z=clf.decision_function(xy).reshape(XX.shape)
#绘制决策边界以及间隔
ax.contour(XX,YY,Z,colors='k',levels=[-1,0,1],alpha=0.5,linestyles=['--','-','--'])
#绘制支持向量
ax.scatter(clf.support_vectors_[:,0],clf.support_vectors_[:,1],s=100,linewidth=1,facecolors='red')
pl.show()