支持向量回归(多核函数)

1.导入模块

import numpy as np
import pandas as pd
from pandas import Series,DataFrame

import matplotlib.pyplot as plt
%matplotlib inline

import sklearn.datasets as datasets

#支持向量回归
from sklearn.svm import SVR

2.生成训练数据

x = np.linspace(-np.pi,np.pi,60)
y = np.sin(x)
#数据加噪
y[::3]+=0.5-np.random.random(20)

X_train = x.reshape(-1,1)
Y_train = y

3.创建支持向量回归模型

svr_linear = SVR(kernel='linear')
svr_rbf = SVR(kernel='rbf')
svr_poly = SVR(kernel='poly')

4.训练数据

svr_linear.fit(X_train,Y_train)
svr_rbf.fit(X_train,Y_train)
svr_poly.fit(X_train,Y_train)

5.与测试数据

#获取预测数据自变量范围
xmin,xmax = X_train.min(),X_train.max()
x_test = np.arange(xmin,xmax,0.01).reshape(-1,1)

#获取预测数据
linear_y_pre = svr_linear.predict(x_test)
rbf_y_pre = svr_rbf.predict(x_test)
poly_y_pre = svr_poly.predict(x_test)

6.绘图

results = [linear_y_pre,rbf_y_pre,poly_y_pre]
titles = ['Linear','rbf','poly']

plt.figure(figsize=(12,12))

for i,result in enumerate(results):
    plt.subplot(3,1,i+1)
    plt.scatter(X_train,Y_train)
    plt.plot(x_test,result,color='orange')
    plt.title(titles[i])

这里写图片描述

  • 0
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值