支持向量机SVM:SVC

Posted st-lovaer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了支持向量机SVM:SVC相关的知识,希望对你有一定的参考价值。

 1 import numpy as np
 2 import matplotlib.pyplot as plt
 3 from sklearn import svm
 4 from sklearn.datasets import make_blobs
 5 X,y=make_blobs(n_samples=50,centers=2,random_state=6)
 6 clf=svm.SVC(kernel=linear,C=1000)
 7 #clf=svm.SVC(kernel=‘rbf‘,C=1000)
 8 clf.fit(X,y)
 9 print("the score of this model:{}".format(clf.score(X,y)))
10 print(clf.predict(X))
11 
12 plt.scatter(X[:,0],X[:,1],c=y,s=30,cmap=plt.cm.Paired)
13 ax=plt.gca()
14 xlim=ax.get_xlim()
15 ylim=ax.get_ylim()
16 
17 xx=np.linspace(xlim[0],xlim[1],30)
18 yy=np.linspace(ylim[0],ylim[1],30)
19 YY,XX=np.meshgrid(yy,xx)
20 xy=np.vstack([XX.ravel(),YY.ravel()]).T
21 z=clf.decision_function(xy).reshape(XX.shape)
22 
23 ax.contour(XX,YY,z,colors=k,levels=[-1,0,1],alpha=0.5,linestyles=[--,-,--])
24 ax.scatter(clf.support_vectors_[:,0],clf.support_vectors_[:,1],s=100,linewidths=1,facecolors=none)
25 plt.show()
 1 X,y=make_blobs(n_samples=50,centers=2,random_state=6)
 2 #clf=svm.SVC(kernel=‘linear‘,C=1000)
 3 clf=svm.SVC(kernel=rbf,C=1000)
 4 clf.fit(X,y)
 5 print("the score of this model:{}".format(clf.score(X,y)))
 6 print(clf.predict(X))
 7 
 8 plt.scatter(X[:,0],X[:,1],c=y,s=30,cmap=plt.cm.Paired)
 9 ax=plt.gca()
10 xlim=ax.get_xlim()
11 ylim=ax.get_ylim()
12 
13 xx=np.linspace(xlim[0],xlim[1],30)
14 yy=np.linspace(ylim[0],ylim[1],30)
15 YY,XX=np.meshgrid(yy,xx)
16 xy=np.vstack([XX.ravel(),YY.ravel()]).T
17 z=clf.decision_function(xy).reshape(XX.shape)
18 
19 ax.contour(XX,YY,z,colors=k,levels=[-1,0,1],alpha=0.5,linestyles=[--,-,--])
20 ax.scatter(clf.support_vectors_[:,0],clf.support_vectors_[:,1],s=100,linewidths=1,facecolors=none)
21 plt.show()
 1 import numpy as np
 2 def make_meshgrid(x,y,h=.02):
 3     x_min = x.min() - 1
 4     x_max = x.max() + 1
 5     y_min = y.min() - 1
 6     y_max = y.max() + 1
 7     xx,yy=np.meshgrid(np.arange(x_min,x_max,h),
 8                       np.arange(y_min,y_max,h))
 9     return xx,yy
10 
11 def plot_contours(ax,clf,xx,yy,**params):
12     z=clf.predict(np.c_[xx.ravel(),yy.ravel()])
13     z=z.reshape(xx.shape)
14     out=ax.contourf(xx,yy,z,**params)
15     return out
16 
17 from sklearn.datasets import load_wine
18 wine=load_wine()
19 X=wine.data[:,:2]
20 y=wine.target
21 C=1.0
22 
23 from sklearn import svm
24 import matplotlib.pyplot as plt
25 models=(svm.SVC(kernel=linear,C=C),
26         svm.LinearSVC(C=C,max_iter=10000),
27         svm.SVC(kernel=rbf,gamma=0.7,C=C),
28         svm.SVC(kernel=poly,degree=3,C=C)
29         )
30 models=(clf.fit(X,y) for clf in models)
31 titles=(SVC with linear kernal L1,
32         LinearSVC(linear kernal) L2,
33         SVC with RBF kernal,
34         SVC with polynomial(degree=3) kernal
35         )
36 fig,sub=plt.subplots(2,2)
37 plt.subplots_adjust(wspace=0.4,hspace=0.4)
38 X0,X1=X[:,0],X[:,1]
39 xx,yy=make_meshgrid(X0,X1)
40 for clf,title,ax in zip(models,titles,sub.flatten()):
41     plot_contours(ax,clf,xx,yy,cmap=plt.cm.plasma,alpha=0.8)
42     ax.scatter(X0,X1,c=y,cmap=plt.cm.plasma,s=20,edgecolors=k)
43     ax.set_xlim(xx.min(),xx.max())
44     ax.set_ylim(yy.min(),yy.max())
45     ax.set_xlabel("Feature 0")
46     ax.set_ylabel("Feature 1")
47     ax.set_xticks(())
48     ax.set_yticks(())
49     ax.set_title(title)
50 plt.show()
 1 models=(svm.SVC(kernel=rbf,gamma=0.1,C=C),
 2         svm.SVC(kernel=rbf,gamma=1,C=C),
 3         svm.SVC(kernel=rbf,gamma=10,C=C),
 4         )
 5 models=(clf.fit(X,y) for clf in models)
 6 titles=(RBF-kernal SVC gamma=0.1,
 7         RBF-kernal SVC gamma=1,
 8         RBF-kernal SVC gamma=10
 9         )
10 fig,sub=plt.subplots(1,3,figsize=(15,3))
11 
12 X0,X1=X[:,0],X[:,1]
13 xx,yy=make_meshgrid(X0,X1)
14 for clf,title,ax in zip(models,titles,sub.flatten()):
15     plot_contours(ax,clf,xx,yy,cmap=plt.cm.plasma,alpha=0.8)
16     ax.scatter(X0,X1,c=y,cmap=plt.cm.plasma,s=20,edgecolors=k)
17     ax.set_xlim(xx.min(),xx.max())
18     ax.set_ylim(yy.min(),yy.max())
19     ax.set_xlabel("Feature 0")
20     ax.set_ylabel("Feature 1")
21     ax.set_xticks(())
22     ax.set_yticks(())
23     ax.set_title(title)
24 plt.show()

 

以上是关于支持向量机SVM:SVC的主要内容,如果未能解决你的问题,请参考以下文章

mooc机器学习第七天-分类支持向量机svm.svc

支持向量机(Support Vector Machine,SVM)

支持向量机大全(SVCSVRSVDDDTSVMTSVMSVCSTM)

吴裕雄 python 机器学习——支持向量机SVM非线性分类SVC模型

SVM支持向量机分类

SVM(支持向量机)