SVM分类方法,svm分三类

  SVM分类方法,svm分三类

  文章目录月亮数据集分类(基于支撑向量机处理)一.2.3.支撑向量机处理鸢尾数据集分类

  月亮数据集分类(基于支撑向量机处理)一。

  1 .绘图函数

  #边界绘制函数importnumpyasnpfrommatplotlib。colorsimportlistedcolormapdefplot _ decision _ boundary(model,axis ) : x0,x1 axis[1],int ) ) axis[1]-axis[0]*100).整形)-1,1 int () axis[3]-axis[2] ) 100).整形1 ) ) #网格函数根据坐标向量返回坐标矩阵x _ new=NP。c _[x0。ravel)x1。ravel((y _ predict=model。预测)

  来自sk学习导入数据集#导入数据集data_x,data _ y=数据集。make _ moons(n _ samples=100,洗牌=真

  froms kle arn。preprocessingimportstandardscaler=standardscaler(#标准化data _ x=scaler。拟合_转换)数据_

  importmatplotlib。pyplotasplt。scatter(data _ x [ data _ y==0,0 ],data _ x [ data _ y==0,1 ] ) PLT.scatca

  2.1 .基于线性核函数

  froms kle arn。svmimportlinearsvcliner _ SVC=线性SVC (c=1e 9,max_iter=100000 ) #线性支撑向量机分类器,iter是重复次数班轮_SVC .的data_y ) #参数和截距plot _ decision _ boundary(liner _ SVC,轴=[-3,3,-3,3 ])

  2 .基于多项式核

  弗洛姆克莱恩。预处理导入多边形功能#多项式回归引入froms kle arn。管道导入管道# python的管道定义多项式多项式SVM " ":参数d:阶:参数丙:规范化常量:返回:管道实例" "返回管道(将#源数据映射到三阶)多项式特征(次数=次数)、#标准化(定标器)标准(标准定标器)、# SVC线性分类器(svm_clf)、线性SVC)c max _ in(poly _ SVC=多项式VC(度=3)poly _ SVC。拟合)数据_ y,数据_y ) #图plot_())为3 #边界血小板计数

  plt.show()print(参数权重)打印(poly _ SVC。命名步骤[ SVM _ clf ].coef_)打印(模型截距)打印(poly _ SVC。命名步骤[ SVM _ clf ].截击_) 3。基于lmdmt核

  从sklearn.svm导入SVC #导入svmdef RBFKernelSVC(伽玛=1.0):返回管道([ (std_scaler ,StandardScaler()),( SVC ,SVC(kernel=rbf ,gamma=gamma)))SVC=RBFKernelSVC(gamma=4)# gamma参数很重要,伽玛参数越大,支持向量越zjdjz.fit(data_x,data _ y)plot _ decision _ boundary(SVC,axis=[-3,3,-3,3])PLT。scatter(data_x[data_y==0,0],data_x[data_y==0,1],color=red)#画点plt.scatter(data_x[data_y==1,0],data_x[data_y==1,1],color=blue)plt.show()

  三。基于支撑向量机处理鸢尾花数据集分类1.导入相关库

  将数组作为公证人从实例导入数据集#导入数据集将matplotlib.pyplot作为血小板计数从sklearn。预处理导入标准比例尺从matplotlib.colors导入已列出色彩映射表2 .编写绘图函数

  # 边界绘制函数def plot _ decision _ boundary(模型,轴):x0,x1=NP。网状网格(NP。Lin space(axis[0],axis[1],int((axis[1]-axis[0])*100))。shape(-1,1),np.linspace(axis[2],axis[3],int((axis[3]-axis[2])*100).整形(-1,1)) #网格函数是从坐标向量中返回坐标矩阵x_new=np.c_[x0.ravel(),x1。ravel()]y _ predict=模型。预测(x _ new)#获取预测值zz=y _预测。形状(x0。shape)custom _ cmap=列出的colormap([ # ef9 a9 a , #FFF59D , #90CAF9]) plt.contourf(x0,x1,ZZ,cmap=custom_cmap) 3 .导入测试数据

  iris=数据集。load _ iris()data _ x=iris。data[:2] data_y=iris.target 4 .数据预处理

  scaler=StandardScaler()#标准化data _ x=scaler。fit _ transform(data _ x)#计算训练数据的均值和方差5.绘制使用数据的散点图

  PLT。RC params[ font。sans-serif ]=[ sim hei ]#用来正常显示中文标签西姆黑是字体名称,字体必须在系统中存在,字体的查看方式和安装第三部分PLT。RC params[轴。unicode _ MINUS ]=False #用来正常显示负号plt.scatter(data_x[data_y==0,0],data_x[data_y==0,1]) #选取y所有为0的X的第一列plt.scatter(data_x[data_y==1,0],data_x[data_y==1,1]) #选取y所有为一的X的第一列plt.scatter(data_x[data_y==2,0],data_x[data_y==2,1]) #选取y所有为2的X的第一列plt.xlabel(萼片长度)#设置横坐标标注轴名为萼片宽度plt.ylabel(萼片宽度)#设置纵坐标标注幅度为萼片长度标题(萼片散点图) # 设置散点图的标题为萼片散点图plt.show()

  1.基于线性核

  从sklearn.svm导入LinearSVCsvc _ line=线性SVC(C=1e 9,max_iter=1000000) #线性支撑向量机分类器svc_line.fit(data_x,data_y)#训练SVM绘图_决策_边界(SVC _ line,axis=[-3,3,-3,4])。scatter(data_x[data_y==0,0],data_x[data_y==0,1])。scatter(data_x[data_y==1,0],data_x[data_y==1,1])plt.show

  2.基于多项式核`

  从sklearn。预处理导入多项式功能#导入多项式回归从sklearn.pipeline导入管道#导入大蟒里的管道极好的多项式SVC(度,c=5):#多项式svm :参数d:阶数:参数丙:正则化常数:返回:一个管道实例返回管道([ #将源数据映射到3阶多项式(多边形_特征,多项式功能(degree=degree)),#标准化( scaler ,StandardScaler()),# SVC线性分类器( svm_clf ,LinearSVC(C=c,loss=hinge ,random_state=42,max _ ITER=100000))])poly _ SVC=多项式SVC(度数=3)poly _ SVC。fit(data _ x,data _ y)plot _ decision _ boundary(poly _ SVC,axis=[-3,3,-3,4])PLT。scatter(data_x[data_y==0,0],data_x[data_y==0,1])

  3.基于lmdmt核

  从sklearn.svm导入SVC #导入svmdef RBFKernelSVC(伽玛=1.0):返回管道([ (std_scaler ,StandardScaler()),( SVC ,SVC(kernel=rbf ,gamma=gamma)))SVC=RBFKernelSVC(gamma=42)# gamma参数很重要,伽玛参数越大,支持向量越zjdjz.fit(data_x,data _ y)plot _ decision _ boundary(SVC,axis=[-3,3,-3,4])PLT。scatter(data_x[data_y==0,0],data_x[data_y==0,1])。散点图(数据x[数据y==1,0],数据x[数据y==1,1])

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: