SVR回归_时间序列分析优缺点

SVR回归_时间序列分析优缺点文章目录1.SVR时间序列预测2.SVR调参3.SVR高斯核与过拟合1.SVR时间序列预测SVR可用于时间序列分析,但不是较好的选择。现在一般采用LSTM神经网络来处理时间序列数据#SVR预测#也可用于时间序列分析(ARIMA也可用于时间序列分析)importnumpyasnpfromsklearnimportsvmimportmatplotlib.pyplotaspltif__name__==”__main__”:#构造数据N=50

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全家桶1年46,售后保障稳定

1.SVR时间序列预测

SVR可用于时间序列分析,但不是较好的选择。现在一般采用LSTM神经网络来处理时间序列数据

# SVR预测
# 也可用于时间序列分析(ARIMA也可用于时间序列分析)
import numpy as np
from sklearn import svm
import matplotlib.pyplot as plt


if __name__ == "__main__":
    # 构造数据
    N = 50
    np.random.seed(0)
    # 排序
    x = np.sort(np.random.uniform(0, 6, N), axis=0)
    y = 2*np.sin(x) + 0.1*np.random.randn(N)
    x = x.reshape(-1, 1)
    print('x =\n', x)
    print('y =\n', y)

    # 高斯核函数
    print('SVR - RBF')
    svr_rbf = svm.SVR(kernel='rbf', gamma=0.2, C=100)
    svr_rbf.fit(x, y)
    # 线性核函数
    print('SVR - Linear')
    svr_linear = svm.SVR(kernel='linear', C=100)
    svr_linear.fit(x, y)
    # 多项式核函数
    print('SVR - Polynomial')
    svr_poly = svm.SVR(kernel='poly', degree=3, C=100)
    svr_poly.fit(x, y)
    print('Fit OK.')

    # 思考:系数1.1改成1.5
    x_test = np.linspace(x.min(), 1.1*x.max(), 100).reshape(-1, 1)
    y_rbf = svr_rbf.predict(x_test)
    y_linear = svr_linear.predict(x_test)
    y_poly = svr_poly.predict(x_test)

    plt.figure(figsize=(9, 8), facecolor='w')
    plt.plot(x_test, y_rbf, 'r-', linewidth=2, label='RBF Kernel')
    plt.plot(x_test, y_linear, 'g-', linewidth=2, label='Linear Kernel')
    plt.plot(x_test, y_poly, 'b-', linewidth=2, label='Polynomial Kernel')
    plt.plot(x, y, 'mo', markersize=6)
    plt.scatter(x[svr_rbf.support_], y[svr_rbf.support_], s=200, c='r', marker='*', label='RBF Support Vectors', zorder=10)
    plt.legend(loc='lower left')
    plt.title('SVR', fontsize=16)
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.grid(True)
    plt.tight_layout(2)
    plt.show()

Jetbrains全家桶1年46,售后保障稳定

在这里插入图片描述

2.SVR调参

# SVR调参
import numpy as np
from sklearn import svm
from sklearn.model_selection import GridSearchCV    # 0.17 grid_search
import matplotlib.pyplot as plt


if __name__ == "__main__":
    N = 50
    np.random.seed(0)
    x = np.sort(np.random.uniform(0, 6, N), axis=0)
    y = 2*np.sin(x) + 0.1*np.random.randn(N)
    x = x.reshape(-1, 1)
    print('x =\n', x)
    print('y =\n', y)

    model = svm.SVR(kernel='rbf')
    # 0.01~100取100个数字
    c_can = np.logspace(-2, 2, 10)
    gamma_can = np.logspace(-2, 2, 10)
    svr = GridSearchCV(model, param_grid={ 
   'C': c_can, 'gamma': gamma_can}, cv=5)
    svr.fit(x, y)
    print('验证参数:\n', svr.best_params_)

    x_test = np.linspace(x.min(), x.max(), 100).reshape(-1, 1)
    y_hat = svr.predict(x_test)

    sp = svr.best_estimator_.support_
    plt.figure(facecolor='w')
    plt.scatter(x[sp], y[sp], s=120, c='r', marker='*', label='Support Vectors', zorder=3)
    plt.plot(x_test, y_hat, 'r-', linewidth=2, label='RBF Kernel')
    plt.plot(x, y, 'go', markersize=5)
    plt.legend(loc='upper right')
    plt.title('SVR', fontsize=16)
    plt.xlabel('X')
    plt.ylabel('Y')
    plt.grid(True)
    plt.show()

在这里插入图片描述

3.SVR高斯核与过拟合

import numpy as np
from sklearn import svm
import matplotlib as mpl
import matplotlib.colors
import matplotlib.pyplot as plt


def extend(a, b):
    big, small = 1.01, 0.01
    return big*a-small*b, big*b-small*a


if __name__ == "__main__":

    t = np.linspace(-5, 5, 6)
    t1, t2 = np.meshgrid(t, t)
    print(t1.ravel().shape)
    # np.stack按给定输出轴连接数组
    x1 = np.stack((t1.ravel(), t2.ravel()), axis=1)
    print(x1.shape)
    N = len(x1)
    x2 = x1 + (1, 1)
    # np.concatenate沿现有轴连接一系列数组
    x = np.concatenate((x1, x2))
    y = np.array([1]*N + [-1]*N)

    clf = svm.SVC(C=0.1, kernel='rbf', gamma=5)
    clf.fit(x, y)
    y_hat = clf.predict(x)
    print('准确率:%.1f%%' % (np.mean(y_hat == y) * 100))

    mpl.rcParams['font.sans-serif'] = [u'SimHei']
    mpl.rcParams['axes.unicode_minus'] = False
    cm_light = mpl.colors.ListedColormap(['#77E0A0', '#FFA0A0'])
    cm_dark = mpl.colors.ListedColormap(['g', 'r'])
    x1_min, x1_max = extend(x[:, 0].min(), x[:, 0].max())  # 第0列的范围
    x2_min, x2_max = extend(x[:, 1].min(), x[:, 1].max())  # 第1列的范围
    x1, x2 = np.mgrid[x1_min:x1_max:300j, x2_min:x2_max:300j]  # 生成网格采样点
    grid_test = np.stack((x1.flat, x2.flat), axis=1)  # 测试点
    grid_hat = clf.predict(grid_test)
    grid_hat.shape = x1.shape  # 使之与输入的形状相同
    plt.figure(facecolor='w')
    plt.pcolormesh(x1, x2, grid_hat, cmap=cm_light)
    plt.scatter(x[:, 0], x[:, 1], s=60, c=y, marker='o', cmap=cm_dark)
    plt.xlim((x1_min, x1_max))
    plt.ylim((x2_min, x2_max))
    plt.title(u'SVM的RBF核与过拟合', fontsize=18)
    plt.tight_layout(0.2)
    plt.show()

在这里插入图片描述


如果对您有帮助,麻烦点赞关注,这真的对我很重要!!!如果需要互关,请评论留言!
在这里插入图片描述


版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/210013.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • MyBatis 快速入门和重点详解(详解)「建议收藏」

    MyBatis 快速入门和重点详解(详解)「建议收藏」目录前言:准备工作:开始:1、创建项目(本博主就使用Eclipse,其他编辑器都可以,工具而已)2、创建数据库(mybatisdemo)及表(student)3、创建User对象4、在entity包下创建userMapper,xml文件,如下图5、创建MyBatis的配置文件6、创建MybatisTest.java进行测试前言:Mybatis概念、名词的…

    2022年6月13日
    28
  • 在Java中常见的数据类型有哪些?「建议收藏」

    在Java中常见的数据类型有哪些?「建议收藏」在java中常见的数据类型有哪些?看图看图看图重要的事情说三遍↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓内置数据类型类型名称 字节、位数 最小值 最大值 默认值 例子 byte字节 1字节,8位 -128(-2^7) 127(2^7-1) 0 bytea=…

    2022年7月8日
    24
  • 什么是gan网络_DAN网络

    什么是gan网络_DAN网络引言GAN,全称GenerativeAdversarialNetworks,中文叫生成式对抗网络,了解GAN,私下我喜欢叫它为“内卷”网络,为啥这么说,我们先来看一个故事!!!01警察与小偷的故事在宇宙的某个星球,某个地方有一个城市,这个城市是一个新兴城市,各种制度建设还不完善,所以城市的治安很混乱,很快,这个城市就出现了很多小偷。当然,现在这批小偷能力层次不齐,有的是盗窃高手,有的是一个毫无技术的小憨憨。小偷盛行,市民投诉反馈,这个城市就开始整治…

    2025年6月27日
    3
  • Scipy randint 与 Numpy randint 的区别[通俗易懂]

    scipy.stats.randint:https://docs.scipy.org/doc/scipy-0.15.1/reference/generated/scipy.stats.randint.htmlnumpy.random.randint:https://docs.scipy.org/doc/numpy/reference/generated/numpy.random.randin…

    2022年4月12日
    52
  • html5移动开发–js温馨提示

    html5移动开发–js温馨提示

    2022年1月15日
    51
  • dsp28335复位电路_28335串口不能中断

    dsp28335复位电路_28335串口不能中断0前言本期实验目标:采用外部中断方式响应按键触发,实现LED电平反转。外部中断是DSP十分常用的功能,通常用来响应一些控制操作,比如判断按键是否按下,传感器是否接收到信号等等。那么通过该例程,大家则可以快速学会使用外部中断的功能!本节仍然将分为硬件部分、软件部分和实验展示三个方面进行介绍。1硬件部分DSP28335支持XINT1-XINT7和XNMI共8路外部中断源,其中中断源XINT1/2和XNMI可以设定为从GPIO端口A的任意一个管脚输入,即GPIO0-GPIO31。而XINT3/4/5/

    2025年9月26日
    6

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号