网络编程
位置:首页>> 网络编程>> Python编程>> python 机器学习之支持向量机非线性回归SVR模型

python 机器学习之支持向量机非线性回归SVR模型

作者:吴裕雄  发布时间:2022-06-17 20:23:55 

标签:python,支持向量机,SVR模型

本文介绍了python 支持向量机非线性回归SVR模型,废话不多说,具体如下:


import numpy as np
import matplotlib.pyplot as plt

from sklearn import datasets, linear_model,svm
from sklearn.model_selection import train_test_split

def load_data_regression():
 '''
 加载用于回归问题的数据集
 '''
 diabetes = datasets.load_diabetes() #使用 scikit-learn 自带的一个糖尿病病人的数据集
 # 拆分成训练集和测试集,测试集大小为原始数据集大小的 1/4
 return train_test_split(diabetes.data,diabetes.target,test_size=0.25,random_state=0)

#支持向量机非线性回归SVR模型
def test_SVR_linear(*data):
 X_train,X_test,y_train,y_test=data
 regr=svm.SVR(kernel='linear')
 regr.fit(X_train,y_train)
 print('Coefficients:%s, intercept %s'%(regr.coef_,regr.intercept_))
 print('Score: %.2f' % regr.score(X_test, y_test))

# 生成用于回归问题的数据集
X_train,X_test,y_train,y_test=load_data_regression()
# 调用 test_LinearSVR
test_SVR_linear(X_train,X_test,y_train,y_test)

python 机器学习之支持向量机非线性回归SVR模型


def test_SVR_poly(*data):
 '''
 测试 多项式核的 SVR 的预测性能随 degree、gamma、coef0 的影响.
 '''
 X_train,X_test,y_train,y_test=data
 fig=plt.figure()
 ### 测试 degree ####
 degrees=range(1,20)
 train_scores=[]
 test_scores=[]
 for degree in degrees:
   regr=svm.SVR(kernel='poly',degree=degree,coef0=1)
   regr.fit(X_train,y_train)
   train_scores.append(regr.score(X_train,y_train))
   test_scores.append(regr.score(X_test, y_test))
 ax=fig.add_subplot(1,3,1)
 ax.plot(degrees,train_scores,label="Training score ",marker='+' )
 ax.plot(degrees,test_scores,label= " Testing score ",marker='o' )
 ax.set_title( "SVR_poly_degree r=1")
 ax.set_xlabel("p")
 ax.set_ylabel("score")
 ax.set_ylim(-1,1.)
 ax.legend(loc="best",framealpha=0.5)

### 测试 gamma,固定 degree为3, coef0 为 1 ####
 gammas=range(1,40)
 train_scores=[]
 test_scores=[]
 for gamma in gammas:
   regr=svm.SVR(kernel='poly',gamma=gamma,degree=3,coef0=1)
   regr.fit(X_train,y_train)
   train_scores.append(regr.score(X_train,y_train))
   test_scores.append(regr.score(X_test, y_test))
 ax=fig.add_subplot(1,3,2)
 ax.plot(gammas,train_scores,label="Training score ",marker='+' )
 ax.plot(gammas,test_scores,label= " Testing score ",marker='o' )
 ax.set_title( "SVR_poly_gamma r=1")
 ax.set_xlabel(r"$\gamma$")
 ax.set_ylabel("score")
 ax.set_ylim(-1,1)
 ax.legend(loc="best",framealpha=0.5)
 ### 测试 r,固定 gamma 为 20,degree为 3 ######
 rs=range(0,20)
 train_scores=[]
 test_scores=[]
 for r in rs:
   regr=svm.SVR(kernel='poly',gamma=20,degree=3,coef0=r)
   regr.fit(X_train,y_train)
   train_scores.append(regr.score(X_train,y_train))
   test_scores.append(regr.score(X_test, y_test))
 ax=fig.add_subplot(1,3,3)
 ax.plot(rs,train_scores,label="Training score ",marker='+' )
 ax.plot(rs,test_scores,label= " Testing score ",marker='o' )
 ax.set_title( "SVR_poly_r gamma=20 degree=3")
 ax.set_xlabel(r"r")
 ax.set_ylabel("score")
 ax.set_ylim(-1,1.)
 ax.legend(loc="best",framealpha=0.5)
 plt.show()

# 调用 test_SVR_poly
test_SVR_poly(X_train,X_test,y_train,y_test)

python 机器学习之支持向量机非线性回归SVR模型


def test_SVR_rbf(*data):
 '''
 测试 高斯核的 SVR 的预测性能随 gamma 参数的影响
 '''
 X_train,X_test,y_train,y_test=data
 gammas=range(1,20)
 train_scores=[]
 test_scores=[]
 for gamma in gammas:
   regr=svm.SVR(kernel='rbf',gamma=gamma)
   regr.fit(X_train,y_train)
   train_scores.append(regr.score(X_train,y_train))
   test_scores.append(regr.score(X_test, y_test))
 fig=plt.figure()
 ax=fig.add_subplot(1,1,1)
 ax.plot(gammas,train_scores,label="Training score ",marker='+' )
 ax.plot(gammas,test_scores,label= " Testing score ",marker='o' )
 ax.set_title( "SVR_rbf")
 ax.set_xlabel(r"$\gamma$")
 ax.set_ylabel("score")
 ax.set_ylim(-1,1)
 ax.legend(loc="best",framealpha=0.5)
 plt.show()

# 调用 test_SVR_rbf
test_SVR_rbf(X_train,X_test,y_train,y_test)

python 机器学习之支持向量机非线性回归SVR模型


def test_SVR_sigmoid(*data):
 '''
 测试 sigmoid 核的 SVR 的预测性能随 gamma、coef0 的影响.
 '''
 X_train,X_test,y_train,y_test=data
 fig=plt.figure()

### 测试 gammam,固定 coef0 为 0.01 ####
 gammas=np.logspace(-1,3)
 train_scores=[]
 test_scores=[]

for gamma in gammas:
   regr=svm.SVR(kernel='sigmoid',gamma=gamma,coef0=0.01)
   regr.fit(X_train,y_train)
   train_scores.append(regr.score(X_train,y_train))
   test_scores.append(regr.score(X_test, y_test))
 ax=fig.add_subplot(1,2,1)
 ax.plot(gammas,train_scores,label="Training score ",marker='+' )
 ax.plot(gammas,test_scores,label= " Testing score ",marker='o' )
 ax.set_title( "SVR_sigmoid_gamma r=0.01")
 ax.set_xscale("log")
 ax.set_xlabel(r"$\gamma$")
 ax.set_ylabel("score")
 ax.set_ylim(-1,1)
 ax.legend(loc="best",framealpha=0.5)
 ### 测试 r ,固定 gamma 为 10 ######
 rs=np.linspace(0,5)
 train_scores=[]
 test_scores=[]

for r in rs:
   regr=svm.SVR(kernel='sigmoid',coef0=r,gamma=10)
   regr.fit(X_train,y_train)
   train_scores.append(regr.score(X_train,y_train))
   test_scores.append(regr.score(X_test, y_test))
 ax=fig.add_subplot(1,2,2)
 ax.plot(rs,train_scores,label="Training score ",marker='+' )
 ax.plot(rs,test_scores,label= " Testing score ",marker='o' )
 ax.set_title( "SVR_sigmoid_r gamma=10")
 ax.set_xlabel(r"r")
 ax.set_ylabel("score")
 ax.set_ylim(-1,1)
 ax.legend(loc="best",framealpha=0.5)
 plt.show()

# 调用 test_SVR_sigmoid
test_SVR_sigmoid(X_train,X_test,y_train,y_test)

python 机器学习之支持向量机非线性回归SVR模型

来源:https://www.cnblogs.com/tszr/p/10799437.html

0
投稿

猜你喜欢

  • gojson是快速解析json数据的一个golang包,你使用它可以快速的查找json内的数据安装 go get github.com/wi
  • 本文实例讲述了JS实现简易图片轮播效果的方法。分享给大家供大家参考。具体如下:这里使用JS制作简易图片轮播效果:制作比较粗糙,使用的图片是w
  • 使用模块requests方式代码如下:import requests url_string="https://******&quo
  • 随着CSS3越来越热,CSS3动画也逐渐受到大家的关注。这次有幸修改淘宝网全站页头,小小地应用了下(详见http://www.taobao.
  • 脚本调试第一步:设置中断(鼠标左键点击)第二步:输入中断条件(可选功能,鼠标右键点击红点)第三步:触发中断(当符合条件是,中断被触发)出现中
  • 有的时候,操作大文件,或者取数,要很久,我们给脚本首尾添加一段代码就知道,这段代码整体的大致运行时间了。import timestart =
  • 是否看见大站的广告都是放在内容中间实现文字环绕的呢,一般普通小站广告只能放在内容开头或者结尾,也许大站的cms系统带这个功能吧,我们小站常用
  • 看了cragle的《有没有必要将网站Div+Css重构?》的文章,有一些想法不说不快,我也在文章的评论里提到曾经开除过两个执着使用div技术
  • 1. 二维(多维)数组降为一维数组方法1: reshape()+concatenate 函数,这个方法是间接法,利用 reshape() 函
  • 我们可使用Haskeys属性判别每个条目是否为一个集合,遍历完整的Request.Cookies集合,以来取得所有cookie的列表及其值:
  • 前言最近在功能性测试的过程中,需要在Python环境下用OpenCV读取网络摄像头的视频流,接着用目标检测器进行视屏帧的后续处理。在测试过程
  • [Q]怎么样查询特殊字符,如通配符%与_ [Q]如何插入单引号到数据库表中 [Q]怎样设置事务一致性 [Q]怎么样利用光标更新数据 [Q]怎
  • python中安装包的方式有很多种:源码包:python setup.py install在线安装:pip install 包名(linux
  • 这个项目到一开始的kickoff到现在,持续了很长的一段时间,现在差不多也接近了尾声,所以要好好做个总结,下面不会设计到太多技术层面上的东西
  • 最近看了下go发送smtp邮件,于是总结一下简单示例 先上一个最简单的代码 (网上搂的代码改了改)package mainimport (
  • 类:在HTML中当表现class属性的时候,人们可以用点(.)号来作为~=号的一个替代选择,所以div.value等同于div[class~
  • 好久没有更新博客了,今天看到论坛上有位朋友问起全屏布局,有点像vc的界面。来了兴趣,就写了一个。运用IE6的怪异模式,通过绝对定位来实现的。
  • 无水印视频下载方法一:无水印视频下载很简单,有一个通用的方法,就是使用去水印平台即可。我使用的去水印平台是:http://douyin.ii
  • 一.错误分类1. 语法错误也称为解析错误,发生在传统编程语言的编译时,在JavaScript中发生在解释时,这些错误是由代码中的意外字符直接
  • 阅读上一篇:[译]Javascript风格要素(一) 我们使用习惯用法可以使我们的意图更加的清晰和简洁。使用==时,当心强制转换考虑下面函数
手机版 网络编程 asp之家 www.aspxhome.com