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 实现文件的递归拷贝实现代码
Aug 02 Python
实例说明Python中比较运算符的使用
May 13 Python
详解Python中的Descriptor描述符类
Jun 14 Python
Python+django实现简单的文件上传
Aug 17 Python
Python增量循环删除MySQL表数据的方法
Sep 23 Python
python学习之面向对象【入门初级篇】
Jan 21 Python
python 识别图片中的文字信息方法
May 10 Python
python实现排序算法解析
Sep 08 Python
python实现旋转和水平翻转的方法
Oct 25 Python
Python遍历字典方式就实例详解
Dec 28 Python
Python selenium页面加载慢超时的解决方案
Mar 18 Python
Python中对象的比较操作==和is区别详析
Feb 12 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 清除网页病毒的方法
2008/12/05 PHP
php+jquery+html实现点击不刷新加载更多的实例代码
2016/08/12 PHP
PHP的CURL方法curl_setopt()函数案例介绍(抓取网页,POST数据)
2016/12/14 PHP
详解php几行代码实现CSV格式文件输出
2017/07/01 PHP
Javascript解决常见浏览器兼容问题的12种方法
2010/01/04 Javascript
更换select下拉菜单背景样式的实现代码
2011/12/20 Javascript
使用Post提交时须将空格转换成加号的解释
2013/01/14 Javascript
jQuery中:not选择器用法实例
2014/12/30 Javascript
jq实现左侧显示图片右侧文字滑动切换效果
2015/08/04 Javascript
实例代码详解javascript实现窗口抖动及qq窗口抖动
2016/01/04 Javascript
微信小程序 开发经验整理
2017/02/15 Javascript
bootstrap PrintThis打印插件使用详解
2017/02/20 Javascript
JS开发常用工具函数(小结)
2019/07/04 Javascript
layui使用button按钮 点击出现弹层 弹层中加载表单的实例
2019/09/04 Javascript
[02:42]决战东方!DOTA2亚洲邀请赛重启荣耀之争
2017/03/17 DOTA
Python DataFrame设置/更改列表字段/元素类型的方法
2018/06/09 Python
Python实现处理逆波兰表达式示例
2018/07/30 Python
Python开发最牛逼的IDE——pycharm
2018/08/01 Python
完美解决python3.7 pip升级 拒绝访问问题
2019/07/12 Python
Python数据库小程序源代码
2019/09/15 Python
Python实现图片裁剪的两种方式(Pillow和OpenCV)
2019/10/30 Python
pycharm的python_stubs问题
2020/04/08 Python
利用CSS3的线性渐变linear-gradient制作边框的示例
2016/06/02 HTML / CSS
Debenhams百货英国官方网站:Debenhams UK
2016/07/12 全球购物
GOOD AMERICAN官网:为曲线性感而设计
2017/12/28 全球购物
The North Face北面德国官网:美国著名户外品牌
2018/12/12 全球购物
Currentbody西班牙:美容仪专家
2019/09/28 全球购物
入党申请自荐书范文
2014/02/11 职场文书
设备动力科岗位职责范本
2014/02/23 职场文书
会议主持词
2014/03/17 职场文书
社区服务活动总结
2014/05/07 职场文书
副科级后备干部考察材料
2014/05/15 职场文书
2015年图书馆个人工作总结
2015/05/26 职场文书
大学学生会主席竞选稿怎么写?
2019/08/19 职场文书
详解Redis集群搭建的三种方式
2021/05/31 Redis
Python读取和写入Excel数据
2022/04/20 Python