整理了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()
更多时间序列预测代码获取:时间序列预测算法全集合--深度学习