机器学习---支持向量机(SVM)算法应用(上)

来源:互联网 发布:c语言float精度 编辑:程序博客网 时间:2024/06/05 16:23

1.SVM线性可分sklearn简单实例

结合上一节实例看
code:

# !/usr/bin/env python# -*- coding: utf-8 -*-# Author: Justin Chanfrom sklearn import svm#三个分类点x = [[2,0],[1,1],[2,3]]#将三个点分成两类,前两个点类别为0,后一个点类别为1y = [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

2.SVM线性可分sklearn复杂实例

# !/usr/bin/env python# -*- coding: utf-8 -*-# Author: Justin Chanimport numpy as np#python中可以把函数画图的模块sudo pip install matplotlibimport pylab as plfrom 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 modelclf = svm.SVC(kernel='linear')clf.fit(X,Y)#下面就是通过生成的点画图#get the separating hyperplanew = 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 planepl.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.39198047626support vectors: [[-1.02126202  0.2408932 ] [-0.46722079 -0.53064123] [ 0.95144703  0.57998206]]clf.coef_: [[ 0.90230696  0.64821811]]

这里写图片描述

原创粉丝点击