python 机器学习之支持向量机非线性回归SVR模型
作者:吴裕雄 发布时间:2022-06-17 20:23:55
标签:python,支持向量机,SVR模型
本文介绍了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)
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)
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)
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)
来源:https://www.cnblogs.com/tszr/p/10799437.html


猜你喜欢
- 测试函数主要是用来评估优化算法特性的,这里我用python3绘制了部分测试函数的图像。具体的测试函数可以结合 * 来了解。想要显示某个测试
- 本文实例为大家分享了python实现udp传输图片的具体代码,供大家参考,具体内容如下首先要了解UDP的工作模式对于服务器,首先绑定IP和端
- 目录一、6个非常重要的str处理词二、重要的str处理 几乎所有的 数据类型里也都能用1、提取字符串中 特定位置的字符2、len 得到当前变
- 一、前提 这里的原则只是针对MySQL数据库,其他的数据库某些是殊途同归,某些还是存在差异。我总结的也是MySQL普遍的规则,对于某些特殊情
- 导读:《我不是药神》是由文牧野执导,徐峥、王传君、周一围、谭卓、章宇、杨新鸣等主演的喜剧电影,于 2018 年 7 月 6 日在中国上映。影
- 1.from_unixtime的语法及用法(1)语法:from_unixtime(timestamp ,date_format)即from_
- 代码如下:# -*- coding: utf-8 -*-#!/usr/bin/python# filename: todo.py# code
- 这些存储过程如下: sp_makewebtask xp_cmdshell xp_dirtree xp_fileexist xp_termin
- PHP Too few arguments to function的解决过去自定义函数的时候如果参数不足,则会抛出一个警告,但是在7.1开始
- 前言CSV(Comma-Separated Values)即逗号分隔值,一种以逗号分隔按行存储的文本文件,所有的值都表现为字符串类型(注意:
- 前言matplotlib.pyplot是一些命令行风格函数的集合,使matplotlib以类似于MATLAB的方式工作。每个pyplot函数
- 可以通过model.state_dict()或者model.named_parameters()函数查看现在的全部可训练参数(包括通过继承得
- Apache2 httpd.conf 中文版 # # 基于 NCSA 服务的配
- 需求表格实现行拖拽,要求只支持同级拖拽!实现使用插件:SortableJS,可以参考官网配置项!// 安装npm install sorta
- 字符串索引示意图字符串切片也就是截取字符串,取子串Python中字符串切片方法字符串[开始索引:结束索引:步长]切取字符串为开始索引到结束索
- python的代码错误检查通常用pep8、pylint和flake8,自动格式化代码通常用autopep8、yapf、black。这些工具均
- 友情提示,您阅读本篇博文的先决条件如下:1、本文示例基于Microsoft SQL Server 2008 R2调测。2、具备 Transa
- 大家中午好,由于过年一直还没回到状态,好久没分享一波小知识了,今天,继续给大家分享一波Python解析日志的小脚本。首先,同样的先看看日志是
- 一 模板语法传值方式一:# urls.pypath('template', views.template)# views.p
- HTTP格式HTTP GET请求的格式:GET /path HTTP/1.1Header1: Value1Header2: Value2He