python机器学习库scikit-learn:SVR的基本应用


Posted in Python onJune 26, 2019

scikit-learn是python的第三方机器学习库,里面集成了大量机器学习的常用方法。例如:贝叶斯,svm,knn等。

scikit-learn的官网 : http://scikit-learn.org/stable/index.html点击打开链接

SVR是支持向量回归(support vector regression)的英文缩写,是支持向量机(SVM)的重要的应用分支。

scikit-learn中提供了基于libsvm的SVR解决方案。

PS:libsvm是台湾大学林智仁教授等开发设计的一个简单、易于使用和快速有效的SVM模式识别与回归的软件包。

我们自己随机产生一些值,然后使用sin函数进行映射,使用SVR对数据进行拟合

from __future__ import division
import time
import numpy as np
from sklearn.svm import SVR
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import learning_curve
import matplotlib.pyplot as plt
 
rng = np.random.RandomState(0)
 
#############################################################################
# 生成随机数据
X = 5 * rng.rand(10000, 1)
y = np.sin(X).ravel()
 
# 在标签中对每50个结果标签添加噪声
 
y[::50] += 2 * (0.5 - rng.rand(int(X.shape[0]/50)))
 
X_plot = np.linspace(0, 5, 100000)[:, None]
 
#############################################################################
# 训练SVR模型
 
#训练规模
train_size = 100
#初始化SVR
svr = GridSearchCV(SVR(kernel='rbf', gamma=0.1), cv=5,
     param_grid={"C": [1e0, 1e1, 1e2, 1e3],
        "gamma": np.logspace(-2, 2, 5)})
#记录训练时间
t0 = time.time()
#训练
svr.fit(X[:train_size], y[:train_size])
svr_fit = time.time() - t0
 
t0 = time.time()
#测试
y_svr = svr.predict(X_plot)
svr_predict = time.time() - t0

然后我们对结果进行可视化处理

#############################################################################
# 对结果进行显示
plt.scatter(X[:100], y[:100], c='k', label='data', zorder=1)
plt.hold('on')
plt.plot(X_plot, y_svr, c='r',
   label='SVR (fit: %.3fs, predict: %.3fs)' % (svr_fit, svr_predict))
 
plt.xlabel('data')
plt.ylabel('target')
plt.title('SVR versus Kernel Ridge')
plt.legend()
 
plt.figure()

python机器学习库scikit-learn:SVR的基本应用

##############################################################################
# 对训练和测试的过程耗时进行可视化
X = 5 * rng.rand(1000000, 1)
y = np.sin(X).ravel()
y[::50] += 2 * (0.5 - rng.rand(int(X.shape[0]/50)))
sizes = np.logspace(1, 4, 7)
for name, estimator in {
      "SVR": SVR(kernel='rbf', C=1e1, gamma=10)}.items():
 train_time = []
 test_time = []
 for train_test_size in sizes:
  t0 = time.time()
  estimator.fit(X[:int(train_test_size)], y[:int(train_test_size)])
  train_time.append(time.time() - t0)
 
  t0 = time.time()
  estimator.predict(X_plot[:1000])
  test_time.append(time.time() - t0)
 
 plt.plot(sizes, train_time, 'o-', color="b" if name == "SVR" else "g",
    label="%s (train)" % name)
 plt.plot(sizes, test_time, 'o--', color="r" if name == "SVR" else "g",
    label="%s (test)" % name)
 
plt.xscale("log")
plt.yscale("log")
plt.xlabel("Train size")
plt.ylabel("Time (seconds)")
plt.title('Execution Time')
plt.legend(loc="best")

python机器学习库scikit-learn:SVR的基本应用

################################################################################
# 对学习过程进行可视化
plt.figure()
 
svr = SVR(kernel='rbf', C=1e1, gamma=0.1)
train_sizes, train_scores_svr, test_scores_svr = \
 learning_curve(svr, X[:100], y[:100], train_sizes=np.linspace(0.1, 1, 10),
     scoring="neg_mean_squared_error", cv=10)
 
plt.plot(train_sizes, -test_scores_svr.mean(1), 'o-', color="r",
   label="SVR")
 
plt.xlabel("Train size")
plt.ylabel("Mean Squared Error")
plt.title('Learning curves')
plt.legend(loc="best")
 
plt.show()

python机器学习库scikit-learn:SVR的基本应用

看见了熟悉的LOSS下降图,我仿佛又回到了学生时代。。

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

Python 相关文章推荐
python实现apahce网站日志分析示例
Apr 02 Python
python3.4下django集成使用xadmin后台的方法
Aug 15 Python
利用Python将时间或时间间隔转为ISO 8601格式方法示例
Sep 05 Python
Python中的默认参数实例分析
Jan 29 Python
基于python分析你的上网行为 看看你平时上网都在干嘛
Aug 13 Python
Centos7下源码安装Python3 及shell 脚本自动安装Python3的教程
Mar 07 Python
Python常见反爬虫机制解决方案
Jun 01 Python
python合并多个excel文件的示例
Sep 23 Python
python Gabor滤波器讲解
Oct 26 Python
python 多线程中join()的作用
Oct 29 Python
python代码实现图书管理系统
Nov 30 Python
Python matplotlib绘制条形统计图 处理多个实验多组观测值
Apr 21 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
Python 数据可视化pyecharts的使用详解
Jun 26 #Python
You might like
php从csv文件读取数据并输出到网页的方法
2015/03/14 PHP
php实现的递归提成方案实例
2015/11/14 PHP
PHP实现留言板功能的详细代码
2017/03/25 PHP
Convert Seconds To Hours
2007/06/16 Javascript
Javascript 去除数组的重复元素
2010/05/04 Javascript
用js来获取上传的文件名纯粹是为了美化而用
2013/10/23 Javascript
动态加载dtree.js树treeview(示例代码)
2013/12/17 Javascript
使用FlexiGrid实现Extjs表格效果方法分享
2014/12/16 Javascript
jquery+ajax+text文本框实现智能提示完整实例
2016/07/09 Javascript
js+html5实现复制文字按钮
2017/07/15 Javascript
清空元素html("") innerHTML="" 与 empty()的区别和应用(推荐)
2017/08/14 Javascript
JS计算两个时间相差分钟数的方法示例
2018/01/10 Javascript
使用vue2实现购物车和地址选配功能
2018/03/29 Javascript
移动端如何用下拉刷新的方式实现上拉加载
2018/12/10 Javascript
[02:25]DOTA2英雄基础教程 生死判决瘟疫法师
2013/12/06 DOTA
[05:15]2018年度CS GO社区贡献奖-完美盛典
2018/12/16 DOTA
浅谈MySQL中的触发器
2015/05/05 Python
Django管理员账号和密码忘记的完美解决方法
2018/12/06 Python
django-allauth入门学习和使用详解
2019/07/03 Python
Python测试模块doctest使用解析
2019/08/10 Python
python命令 -u参数用法解析
2019/10/24 Python
python实现将一维列表转换为多维列表(numpy+reshape)
2019/11/29 Python
python安装第三方库如xlrd的方法
2020/10/31 Python
详解python日志输出使用配置文件格式
2021/02/10 Python
python 基于pygame实现俄罗斯方块
2021/03/02 Python
CSS3属性background-size使用指南
2014/12/09 HTML / CSS
Ted Baker英国官网:男士和女士服装及配件
2017/03/13 全球购物
国际奢侈品品牌童装购物网站:Designer Childrenswear
2019/05/08 全球购物
财务管理专业应届毕业生求职信
2013/09/22 职场文书
电大自我鉴定范文
2013/10/01 职场文书
家长写给孩子的评语
2014/04/18 职场文书
公安四风对照检查材料思想汇报
2014/10/11 职场文书
一年级数学上册复习计划
2015/01/17 职场文书
2016春节家属慰问信
2015/03/25 职场文书
Nginx实现高可用集群构建(Keepalived+Haproxy+Nginx)
2021/05/27 Servers
详细聊一聊mysql的树形结构存储以及查询
2022/04/05 MySQL