结合上一节实例看
code:
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Justin Chan
from sklearn import svm
#三个分类点
x = [[2,0],[1,1],[2,3]]
#将三个点分成两类,前两个点类别为0,后一个点类别为1
y = [0,0,1]
clf = svm.SVC(kernel='linear')
clf.fit(x,y)
print(clf)
#get support vectors获取支持向量点列表
print(clf.support_vectors_)
#get indices of support vectors获取支持向量点在向量列表中的索引值
print(clf.support_)
#get number of support vectors for each class获取每一类中支持向量点的个数
print(clf.n_support_)
#预测分类点[3,0]
print(clf.predict([[3,0]]))
result:
SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0,
decision_function_shape='ovr', degree=3, gamma='auto', kernel='linear',
max_iter=-1, probability=False, random_state=None, shrinking=True,
tol=0.001, verbose=False)
[[ 1. 1.]
[ 2. 3.]]#支持向量点坐标分别为[1,1][2,3]
[1 2]#支持向量点的索引是1和2,另一个索引0的点[2,0]不是支持向量点
[1 1]#两类都各有一个支持向量点
[0]#预测分类点类别为0
# !/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Justin Chan
import numpy as np
#python中可以把函数画图的模块sudo pip install matplotlib
import pylab as pl
from sklearn import svm
#we creat 40 separable points
#seed随机生成点,这里给定一个参数0,固定住随机生成的结果,保证每次运行出来的随机值是一致的。
np.random.seed(0)
#随机产生20个点,-[2,2]均值为2,方差为2,正态分布在左侧,+[2,2]均值为2,方差为2,正态分布在右侧
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]
#产生从-5到5一些连续的值
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
#三条直线是平行的,斜率一样,截距不同yy是中间的线,yy_down和yy_up分别是支持向量的线
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_)
#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--')
#使用scatter把support vectors的点单独圈出来
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()
result:
w: [ 0.90230696 0.64821811]
a: -1.39198047626
support vectors: [[-1.02126202 0.2408932 ]
[-0.46722079 -0.53064123]
[ 0.95144703 0.57998206]]
clf.coef_: [[ 0.90230696 0.64821811]]