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中装饰器的使用
Jul 12 Python
Linux下python制作名片示例
Jul 20 Python
numpy中loadtxt 的用法详解
Aug 03 Python
对python tkinter窗口弹出置顶的方法详解
Jun 14 Python
Django分页功能的实现代码详解
Jul 29 Python
Python获取一个用户名的组ID过程解析
Sep 03 Python
python logging添加filter教程
Dec 24 Python
python Tensor和Array对比分析
Jan 08 Python
PyTorch之nn.ReLU与F.ReLU的区别介绍
Jun 27 Python
python判断all函数输出结果是否为true的方法
Dec 03 Python
使用python操作lmdb对数据读取的实例
Dec 11 Python
sklearn中的交叉验证的实现(Cross-Validation)
Feb 22 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/06/14 PHP
PHP中PDO的错误处理
2011/09/04 PHP
浏览器预览PHP文件时顶部出现空白影响布局分析原因及解决办法
2013/01/11 PHP
PHP输出日历表代码实例
2015/03/27 PHP
PHP开启opcache提升代码性能
2015/04/26 PHP
Yii2实现ActiveForm ajax提交
2017/05/26 PHP
javascript实现2016新年版日历
2016/01/25 Javascript
纯JS代码实现气泡效果
2016/05/04 Javascript
浅谈JS正则表达式的RegExp对象和括号的使用
2016/07/28 Javascript
自定义require函数让浏览器按需加载Js文件
2016/11/24 Javascript
微信小程序三级联动地址选择器的实例代码
2017/07/12 Javascript
详解使用jest对vue项目进行单元测试
2018/09/07 Javascript
JS实现获取当前所在周的周六、周日示例分析
2019/05/11 Javascript
基于JavaScript实现表格隔行换色
2020/05/08 Javascript
Python中的生成器和yield详细介绍
2015/01/09 Python
使用IPython下的Net-SNMP来管理类UNIX系统的教程
2015/04/15 Python
简单谈谈python中的多进程
2016/11/06 Python
Python编程使用tkinter模块实现计算器软件完整代码示例
2017/11/29 Python
Python读取英文文件并记录每个单词出现次数后降序输出示例
2018/06/28 Python
python 解压pkl文件的方法
2018/10/25 Python
用python3教你任意Html主内容提取功能
2018/11/05 Python
Python3.4解释器用法简单示例
2019/03/22 Python
一文了解Python并发编程的工程实现方法
2019/05/31 Python
python迭代器常见用法实例分析
2019/11/22 Python
python调用HEG工具批量处理MODIS数据的方法及注意事项
2020/02/18 Python
用ldap作为django后端用户登录验证的实现
2020/12/07 Python
Darphin迪梵官网: 来自巴黎,植物和精油调制的护肤品牌
2016/10/11 全球购物
美国婚礼和派对礼品网站:Kate Aspen(新娘送礼会、迎婴派对)
2018/03/28 全球购物
俄罗斯品牌服装和鞋子的在线商店:KUPIVIP
2019/10/27 全球购物
项目资料员岗位职责
2013/12/10 职场文书
计算机大学生职业生涯规划书范文
2014/02/19 职场文书
大学生优秀自荐信范文
2014/02/25 职场文书
机动车交通事故协议书
2015/01/29 职场文书
2015年学校消防安全工作总结
2015/10/14 职场文书
2016年庆祝六一儿童节活动总结
2016/04/06 职场文书
浅析python中特殊文件和特殊函数
2022/02/24 Python