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编写web API的教程
Apr 30 Python
python函数局部变量用法实例分析
Aug 04 Python
Python贪吃蛇游戏编写代码
Oct 26 Python
python中将函数赋值给变量时需要注意的一些问题
Aug 18 Python
pandas apply 函数 实现多进程的示例讲解
Apr 20 Python
python+POP3实现批量下载邮件附件
Jun 19 Python
Python递归函数 二分查找算法实现解析
Aug 12 Python
python基于opencv检测程序运行效率
Dec 28 Python
Django 解决由save方法引发的错误
May 21 Python
Python利用pip安装tar.gz格式的离线资源包
Sep 14 Python
详解Pycharm第三方库的安装及使用方法
Dec 29 Python
Python机器学习之基础概述
May 19 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
Terran剧情介绍
2020/03/14 星际争霸
浅谈php://filter的妙用
2019/03/05 PHP
JavaScript 学习历程和心得分享
2010/12/12 Javascript
js弹出窗口之弹出层的小例子
2013/06/17 Javascript
js使浏览器窗口最大化实现代码(适用于IE)
2013/08/07 Javascript
JavaScript实现鼠标点击后层展开效果的方法
2015/05/13 Javascript
分享jQuery封装好的一些常用操作
2016/07/28 Javascript
jQuery插件EasyUI实现Layout框架页面中弹出窗体到最顶层效果(穿越iframe)
2016/08/05 Javascript
JavaScript使用forEach()与jQuery使用each遍历数组时return false 的区别
2016/08/26 Javascript
js 点击a标签 获取a的自定义属性方法
2016/11/21 Javascript
js实现九宫格拼图小游戏
2017/02/13 Javascript
js实现鼠标拖动功能
2017/03/20 Javascript
详解angularJs中关于ng-class的三种使用方式说明
2017/06/02 Javascript
详解TypeScript+Vue 插件 vue-class-component的使用总结
2019/02/18 Javascript
js实现旋转的星空效果
2019/11/01 Javascript
[02:53]2018年度DOTA2最佳战队-完美盛典
2018/12/17 DOTA
python发送arp欺骗攻击代码分析
2014/01/16 Python
Python守护进程用法实例分析
2015/06/04 Python
Python中字符串格式化str.format的详细介绍
2017/02/17 Python
Python中turtle作图示例
2017/11/15 Python
Python基于更相减损术实现求解最大公约数的方法
2018/04/04 Python
Python代码缩进和测试模块示例详解
2018/05/07 Python
在python中使用with打开多个文件的方法
2019/01/07 Python
python f-string式格式化听语音流程讲解
2019/06/18 Python
python实现身份证实名认证的方法实例
2019/11/08 Python
Python使用psutil获取进程信息的例子
2019/12/17 Python
pycharm运行程序时看不到任何结果显示的解决
2020/02/21 Python
Python+Appium实现自动化测试的使用步骤
2020/03/24 Python
如何利用Python识别图片中的文字
2020/05/31 Python
python 实现rolling和apply函数的向下取值操作
2020/06/08 Python
什么是python的函数体
2020/06/19 Python
css3弹性盒模型实例介绍
2013/05/27 HTML / CSS
Farfetch阿联酋:奢侈品牌时尚购物平台
2019/07/26 全球购物
文化活动实施方案
2014/03/28 职场文书
2015年依法治校工作总结
2015/07/27 职场文书
2016年寒假政治学习心得体会
2015/10/09 职场文书