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


Posted in Python onJune 26, 2019

本文介绍了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模型

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python批量提交沙箱问题实例
Oct 08 Python
详解Python中映射类型的内建函数和工厂函数
Aug 19 Python
python利用OpenCV2实现人脸检测
Apr 16 Python
Python数字图像处理之霍夫线变换实现详解
Jan 12 Python
Python3.5.3下配置opencv3.2.0的操作方法
Apr 02 Python
win10 64bit下python NLTK安装教程
Sep 19 Python
python数组循环处理方法
Aug 26 Python
使用Python实现正态分布、正态分布采样
Nov 20 Python
python json.dumps() json.dump()的区别详解
Jul 14 Python
Python设计密码强度校验程序
Jul 30 Python
Python 整行读取文本方法并去掉readlines换行\n操作
Sep 03 Python
python爬虫利器之requests库的用法(超全面的爬取网页案例)
Dec 17 Python
python机器学习库scikit-learn:SVR的基本应用
Jun 26 #Python
Python Numpy 实现交换两行和两列的方法
Jun 26 #Python
python 字典操作提取key,value的方法
Jun 26 #Python
通过PYTHON来实现图像分割详解
Jun 26 #Python
Flask模板引擎之Jinja2语法介绍
Jun 26 #Python
如何使用Python实现自动化水军评论
Jun 26 #Python
详解用pyecharts Geo实现动态数据热力图城市找不到问题解决
Jun 26 #Python
You might like
PHP备份数据库生成SQL文件并下载的函数代码
2012/02/05 PHP
php过滤敏感词的示例
2014/03/31 PHP
php调用淘宝开放API实现根据卖家昵称获取卖家店铺ID的方法
2015/07/29 PHP
Zend Framework入门教程之Zend_Mail用法示例
2016/12/08 PHP
PHP实现本地图片转base64格式并上传
2020/05/29 PHP
document 和 document.all 分别什么时候用
2006/06/22 Javascript
JavaScript 字符串处理函数使用小结
2010/12/02 Javascript
jquery解决图片路径不存在执行替换路径
2013/02/06 Javascript
Json实现异步请求提交评论无需跳转其他页面
2014/10/11 Javascript
nodejs下打包模块archiver详解
2014/12/03 NodeJs
nodejs6下使用koa2框架实例
2017/05/18 NodeJs
jQuery模拟爆炸倒计时功能实例代码
2017/08/21 jQuery
详解react-redux插件入门
2018/04/19 Javascript
vue实现分页组件
2020/06/16 Javascript
webpack4 SplitChunks实现代码分隔详解
2019/05/23 Javascript
基于vue实现探探滑动组件功能
2020/05/29 Javascript
Nodejs 微信小程序消息推送的实现
2021/01/20 NodeJs
Python文本相似性计算之编辑距离详解
2016/11/28 Python
Go/Python/Erlang编程语言对比分析及示例代码
2018/04/23 Python
python 接收处理外带的参数方法
2018/12/03 Python
python将txt文件读取为字典的示例
2018/12/22 Python
在Python中调用Ping命令,批量IP的方法
2019/01/26 Python
python设计tcp数据包协议类的例子
2019/07/23 Python
Python交互式图形编程的实现
2019/07/25 Python
pytorch实现特殊的Module--Sqeuential三种写法
2020/01/15 Python
Python3.7在anaconda里面使用IDLE编译器的步骤详解
2020/04/29 Python
解决pyinstaller 打包exe文件太大,用pipenv 缩小exe的问题
2020/07/13 Python
Python虚拟环境virtualenv创建及使用过程图解
2020/12/08 Python
html5理解head_动力节点Java学院整理
2017/07/13 HTML / CSS
Laura官网:加拿大女性的顶级时尚目的地
2019/09/20 全球购物
Yahoo-PHP面试题4
2012/05/05 面试题
学校采购员岗位职责
2014/01/02 职场文书
领导干部群众路线教育实践活动剖析材料
2014/10/10 职场文书
具结保证书
2015/01/17 职场文书
能让Python提速超40倍的神器Cython详解
2021/06/24 Python
Win11软件图标固定到任务栏
2022/04/19 数码科技