[开源]基于SVM的时间序列预测python代码

整理了SVM的时间序列预测python代码分享给大家。记得点赞哦

#!/usr/bin/env python
# coding: utf-8



import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn import preprocessing
from sklearn.metrics import mean_squared_error
from math import sqrt
from sklearn import preprocessing
from sklearn.svm import SVR
from sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score
import math
from numpy import concatenate




feanum=1
window=5
Ra = 0.8
df1=pd.read_csv('shao - 单.csv', usecols=[1]) #读取数据
train_d, test_d = df1[0:int(len(df1)*Ra)], df1[int(len(df1)*Ra):]



min_max_scaler = preprocessing.MinMaxScaler()
df0=min_max_scaler.fit_transform(df1)
df = pd.DataFrame(df0, columns=df1.columns)




#这一部分在处理数据 将原始数据改造为模型需要的输入
stock=df
seq_len=window
amount_of_features = len(stock.columns)#有几列
data = stock.values #pd.DataFrame(stock) 表格转化为矩阵
sequence_length = seq_len + 1#序列长度+1
result = []
for index in range(len(data) - sequence_length):#循环 数据长度-时间窗长度 次
    result.append(data[index: index + sequence_length])#第i行到i+5
result = np.array(result)#得到样本,样本形式为 window*feanum

cut=len(test_d)
train = result[:-cut, :]
x_train = train[:, :-1]
y_train = train[:, -1][:,-1]
x_test = result[-cut:, :-1]
y_test = result[-cut:, -1][:,-1]
X_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1], amount_of_features))
X_test = np.reshape(x_test, (x_test.shape[0], x_test.shape[1], amount_of_features))  


print("X_train", X_train.shape)
print("y_train", y_train.shape)
print("X_test", X_test.shape)
print("y_test", y_test.shape)



X_train=X_train.reshape(len(X_train),window)
y_train=y_train.reshape(len(X_train))
X_test=X_test.reshape(cut,window)
y_test=y_test.reshape(cut)
print("X_train", X_train.shape)
print("y_train", y_train.shape)
print("X_test", X_test.shape)
print("y_test", y_test.shape)


svr = SVR(C=1.0, cache_size=200,degree=3, gamma=0.01,kernel='linear', max_iter=-1) 
model = svr.fit(X_train, y_train)


#在训练集上的拟合结果
y_train_predict=model.predict(X_train)




draw=pd.concat([pd.DataFrame(y_train),pd.DataFrame(y_train_predict)],axis=1)
draw.iloc[0:len(train_d),0].plot(figsize=(12,6))
draw.iloc[0:len(train_d),1].plot(figsize=(12,6))
plt.legend(('real', 'predict'),loc='upper right',fontsize='15')
plt.title("Train Data",fontsize='30') 




#在测试集上的预测
y_test_predict=model.predict(X_test)


X = pd.DataFrame(y_test)
Y = pd.DataFrame(y_test_predict)
X = min_max_scaler.inverse_transform(X)
Y = min_max_scaler.inverse_transform(Y)




testScore = math.sqrt(mean_squared_error(X, Y))
print('RMSE为:%.3f ' %(testScore))
testScore = mean_absolute_error(X, Y)
print('MAE为:%.3f ' %(testScore))
testScore = r2_score(X, Y)
print('R2为:%.3f ' %(testScore))



plt.figure(figsize=(10, 4),dpi=150)
plt.plot(X, label="Actual", color='red',linewidth=4)
plt.plot(Y, color='blue',label='Prediction',linewidth=2.5,linestyle="--")
plt.title('Prediction', size=15)
plt.ylabel('DO',size=15)
plt.xlabel('time',size=15)
plt.legend()
plt.show()




更多时间序列预测代码获取时间序列预测算法全集合--深度学习

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-04-08 07:34:03       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-04-08 07:34:03       100 阅读
  3. 在Django里面运行非项目文件

    2024-04-08 07:34:03       82 阅读
  4. Python语言-面向对象

    2024-04-08 07:34:03       91 阅读

热门阅读

  1. C语言学习分享

    2024-04-08 07:34:03       28 阅读
  2. 什么是物联网?

    2024-04-08 07:34:03       37 阅读
  3. 小程序View点击响应传递多个参数

    2024-04-08 07:34:03       32 阅读
  4. 微信小程序脚本的执行顺序

    2024-04-08 07:34:03       29 阅读
  5. KADB锁冲突查看及解决

    2024-04-08 07:34:03       31 阅读
  6. 金融数据_Scikit-Learn决策树(DecisionTreeClassifier)实例

    2024-04-08 07:34:03       29 阅读
  7. 【面经】软件开发工程师-后端方向1

    2024-04-08 07:34:03       26 阅读
  8. 2024-04-07(复盘前端)

    2024-04-08 07:34:03       30 阅读
  9. MCU电子方案开发

    2024-04-08 07:34:03       31 阅读
  10. Vue3 + TS 按需引入和全局引入 Echarts

    2024-04-08 07:34:03       34 阅读
  11. FPGA与数字图像处理专栏分类与索引

    2024-04-08 07:34:03       31 阅读
  12. 【rabbitmq】rabbitmq与erlang的版本对应关系

    2024-04-08 07:34:03       32 阅读
  13. c++ const关键词介绍

    2024-04-08 07:34:03       39 阅读