时间序列预测——GRU

  本文展示了使用GRU进行时间序列预测的全过程,包含详细的注释。整个过程主要包括:数据导入、数据清洗、结构转化、建立GRU模型、训练模型(包括动态调整学习率和earlystopping的设置)、预测、结果展示、误差评估等完整的时间序列预测流程。
  本文使用的数据集在本人上传的资源中,链接为mock_kaggle.csv

import pandas as pd
import numpy as np
import math
from matplotlib import pyplot as plt
from matplotlib.pylab import mpl
import tensorflow as tf
from sklearn.preprocessing import MinMaxScaler
from keras import backend as K
from keras.layers import LeakyReLU
from sklearn.metrics import mean_squared_error # 均方误差
from keras.callbacks import LearningRateScheduler
from keras.callbacks import EarlyStopping
from tensorflow.keras import Input, Model,Sequential
mpl.rcParams['font.sans-serif'] = ['SimHei']   #显示中文
mpl.rcParams['axes.unicode_minus']=False       #显示负号

取数据

data=pd.read_csv('mock_kaggle.csv',encoding ='gbk',parse_dates=['datetime'])
Date=pd.to_datetime(data.datetime)
data['date'] = Date.map(lambda x: x.strftime('%Y-%m-%d'))
datanew=data.set_index(Date)
series = pd.Series(datanew['股票'].values, index=datanew['date'])
series
date
2014-01-01    4972
2014-01-02    4902
2014-01-03    4843
2014-01-04    4750
2014-01-05    4654
              ... 
2016-07-27    3179
2016-07-28    3071
2016-07-29    4095
2016-07-30    3825
2016-07-31    3642
Length: 937, dtype: int64

滞后扩充数据

dataframe1 = pd.DataFrame()
num_hour = 16
for i in range(num_hour,0,-1):
    dataframe1['t-'+str(i)] = series.shift(i)
dataframe1['t'] = series.values
dataframe3=dataframe1.dropna()
dataframe3.index=range(len(dataframe3))
dataframe3
t-16 t-15 t-14 t-13 t-12 t-11 t-10 t-9 t-8 t-7 t-6 t-5 t-4 t-3 t-2 t-1 t
0 4972.0 4902.0 4843.0 4750.0 4654.0 4509.0 4329.0 4104.0 4459.0 5043.0 5239.0 5118.0 4984.0 4904.0 4822.0 4728.0 4464
1 4902.0 4843.0 4750.0 4654.0 4509.0 4329.0 4104.0 4459.0 5043.0 5239.0 5118.0 4984.0 4904.0 4822.0 4728.0 4464.0 4265
2 4843.0 4750.0 4654.0 4509.0 4329.0 4104.0 4459.0 5043.0 5239.0 5118.0 4984.0 4904.0 4822.0 4728.0 4464.0 4265.0 4161
3 4750.0 4654.0 4509.0 4329.0 4104.0 4459.0 5043.0 5239.0 5118.0 4984.0 4904.0 4822.0 4728.0 4464.0 4265.0 4161.0 4091
4 4654.0 4509.0 4329.0 4104.0 4459.0 5043.0 5239.0 5118.0 4984.0 4904.0 4822.0 4728.0 4464.0 4265.0 4161.0 4091.0 3964
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
916 1939.0 1967.0 1670.0 1532.0 1343.0 1022.0 813.0 1420.0 1359.0 1075.0 1015.0 917.0 1550.0 1420.0 1358.0 2893.0 3179
917 1967.0 1670.0 1532.0 1343.0 1022.0 813.0 1420.0 1359.0 1075.0 1015.0 917.0 1550.0 1420.0 1358.0 2893.0 3179.0 3071
918 1670.0 1532.0 1343.0 1022.0 813.0 1420.0 1359.0 1075.0 1015.0 917.0 1550.0 1420.0 1358.0 2893.0 3179.0 3071.0 4095
919 1532.0 1343.0 1022.0 813.0 1420.0 1359.0 1075.0 1015.0 917.0 1550.0 1420.0 1358.0 2893.0 3179.0 3071.0 4095.0 3825
920 1343.0 1022.0 813.0 1420.0 1359.0 1075.0 1015.0 917.0 1550.0 1420.0 1358.0 2893.0 3179.0 3071.0 4095.0 3825.0 3642

921 rows × 17 columns

二折划分数据并标准化

# pot=int(len(dataframe3)*0.8)
pd.DataFrame(np.random.shuffle(dataframe3.values))  #shuffle
pot=len(dataframe3)-12
train=dataframe3[:pot]
test=dataframe3[pot:]
scaler = MinMaxScaler(feature_range=(0, 1)).fit(train)
#scaler = preprocessing.StandardScaler().fit(train)
train_norm
  • 26
    点赞
  • 214
    收藏
    觉得还不错? 一键收藏
  • 39
    评论
GRU(门控循环单元)是一种循环神经网络(RNN)的变体,它具有比标准RNN更好的长期记忆能力。GRU使用门控机制来控制信息的流动,从而防止信息的过度或不足。在时间序列预测中,我们可以使用GRU预测未来的时间点。 以下是使用Python和Keras库进行时间序列预测的示例代码: 首先,我们需要导入所需的库: ```python import pandas as pd import numpy as np import matplotlib.pyplot as plt from keras.models import Sequential from keras.layers import Dense from keras.layers import GRU ``` 接下来,我们需要加载和预处理时间序列数据: ```python # load the data data = pd.read_csv('time_series_data.csv') # convert the data to a numpy array data = data.values # split the data into training and testing sets train_size = int(len(data) * 0.8) train_data = data[0:train_size,:] test_data = data[train_size:len(data),:] # normalize the data min_value = np.min(train_data) max_value = np.max(train_data) train_data = (train_data - min_value) / (max_value - min_value) test_data = (test_data - min_value) / (max_value - min_value) ``` 然后,我们需要准备输入和输出数据: ```python # prepare the input and output data def prepare_data(data, time_steps): X, Y = [], [] for i in range(len(data)-time_steps-1): X.append(data[i:(i+time_steps), 0]) Y.append(data[(i+time_steps), 0]) return np.array(X), np.array(Y) time_steps = 3 train_X, train_Y = prepare_data(train_data, time_steps) test_X, test_Y = prepare_data(test_data, time_steps) ``` 接下来,我们需要构建和训练GRU模型: ```python # create the GRU model model = Sequential() model.add(GRU(50, input_shape=(time_steps, 1))) model.add(Dense(1)) model.compile(loss='mean_squared_error', optimizer='adam') # train the model model.fit(train_X, train_Y, epochs=100, batch_size=10) ``` 最后,我们可以使用训练好的模型进行预测: ```python # make predictions train_predict = model.predict(train_X) test_predict = model.predict(test_X) # invert the predictions train_predict = (train_predict * (max_value - min_value)) + min_value train_Y = (train_Y * (max_value - min_value)) + min_value test_predict = (test_predict * (max_value - min_value)) + min_value test_Y = (test_Y * (max_value - min_value)) + min_value ``` 我们可以使用以下代码绘制预测结果和实际结果: ```python # plot the results plt.plot(train_Y) plt.plot(train_predict) plt.plot(test_Y) plt.plot(test_predict) plt.show() ``` 这样,我们就可以使用Python和Keras库进行时间序列预测了。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值