LSTM时序数据预测实践(实时股票数据)
LSTM时序数据预测实践(实时股票数据)
预测结果展示(以红线分割,红线前数据参与训练,红线后数据未参与训练):
红线以后可以看到随着预测时间段的加长,预测误差会越来越大。
-
获取最新股票数据
import pandas_datareader.data as web # 读取实时股票数据的接口 import datetime from collections import deque import numpy as np import pandas as pd import torch import torch.nn as nn from torch.nn import MSELoss from torch.optim import Adam,SGD,RMSprop from torch.utils.data import DataLoader,Dataset from sklearn.model_selection import train_test_split from sklearn.preprocessing import MinMaxScaler import matplotlib.pyplot as plt start = datetime.datetime(year=2000,month=1,day=1) end = datetime.datetime(year=2022,month=3,day=16) df = web.DataReader(name='^DJI',data_source='stooq',start=start,end=end) # 获取道琼斯工业最新股票数据 df.sort_index(inplace=True) df.dropna(inplace=True) shift_size = -1 df['label'] = df['Close'].shift(periods=shift_size) # 以下一日收盘价为预测目标 df.set_index(keys='Date',inplace=True) df ''' Open High Low Close Volume label(下-日收盘价) Date 2000-01-03 11501.80 11522.00 11305.70 11357.50 169680388 10997.90 2000-01-04 11349.80 11350.10 10986.50 10997.90 178357418 11122.70 2000-01-05 10989.40 11215.10 10938.70 11122.70 203266571 11253.30 2000-01-06 11113.40 11313.50 11098.50 11253.30 176642517 11522.60 2000-01-07 11247.10 11528.10 11239.90 11522.60 184926808 11572.20 ... ... ... ... ... ... ... 2022-03-09 32860.42 33457.28 32860.42 33286.25 507633430 33174.07 2022-03-10 33106.77 33236.59 32819.76 33174.07 462887778 32944.19 2022-03-11 33279.72 33515.61 32911.89 32944.19 432875928 32945.24 2022-03-14 33000.37 33395.59 32818.16 32945.24 475399572 33544.34 2022-03-15 32989.27 33620.84 32989.27 33544.34 466174065 '''
-
数据拆分
# 拆分训练集和验证集(数据较大,进行归一化处理) X = df.loc[df['label'].notnull(),df.columns[:-1]] Y = df.loc[df['label'].notnull(),df.columns[-1]] X_scaler = MinMaxScaler() Y_scaler = MinMaxScaler() X = X_scaler.fit_transform(X) Y = Y_scaler.fit_transform(np.array(Y).reshape(-1,1)) train_x,test_x,train_y,test_y = train_test_split(X,Y,test_size=0.1,shuffle=False) # shuffle设置为False,不打乱时序数据顺序
-
构建时序数据加载器
class MY_Dataset(Dataset): def __init__(self,data,label,seq_len): self.data = data self.label = label self.seq_len = seq_len # 序列长度,就是每一次扔几天连续股票数据到模型参与训练 self.len_ = data.shape[0]-seq_len def __getitem__(self,index): return torch.tensor(self.data[index:index+self.seq_len],dtype=torch.float32),torch.tensor(self.label[index:index+self.seq_len],dtype=torch.float32) def __len__(self): return self.len_ # 形成序列数据集 seq_len = 14 # 设置序列为连续2周时间内的股票数据 data_train = MY_Dataset(data=train_x,label=train_y,seq_len=seq_len) # 设置批次 batch_size = 10 dataloader = DataLoader(dataset=data_train,batch_size=batch_size,shuffle=True,drop_last=True) # 这里shuffle可以设置为True,只要连续seq_len不被打乱就行
-
模型创建
# 创建LSTM模型 class Lstm(nn.Module): def __init__(self,feature_size,hidden_size,num_layers): super(Lstm,self).__init__() self.lstm = nn.LSTM(input_size=feature_size,hidden_size=hidden_size,num_layers=num_layers,batch_first=True,dropout=0.02) self.linear = nn.Linear(in_features=hidden_size,out_features=1) def forward(self,x,h0,c0): out,(hn,cn)= self.lstm(x,(h0,c0)) z = self.linear(out) return z,(hn,cn)
-
创建损失函数和优化器
# 实例化模型 feature_size = 5 # 预测特征为[Open,High,Low,Close,Volume]5个维度 hidden_size = 100 num_layers = 2 # 设置的双层LSTM lstm = Lstm(feature_size=feature_size,hidden_size=hidden_size,num_layers=num_layers) for i in lstm.parameters(): if i.dim()>=2: nn.init.xavier_normal_(i) # 设置损失函数和优化函数 Loss = MSELoss() opt = Adam(params=lstm.parameters(),lr=0.01)
-
训练
# 开始训练 epochs = 50 # 先简单50轮 h0 = torch.zeros(size=(num_layers,batch_size,hidden_size)) c0 = torch.zeros(size=(num_layers,batch_size,hidden_size)) res = [] for epoch in range(epochs): for x,y in dataloader: out,(hn,cn)= lstm(x,h0,c0) ## 每批次训练的初始h0,c0考虑怎样设置合理??? # h0,c0 = hn.detach(),cn.detach() loss_value = Loss(out,y) opt.zero_grad() loss_value.backward() opt.step() print(loss_value) res.append(loss_value)
-
模型预测
# 将全部数据进行预测(其中后500多天的数据未参与训练,属于纯预测) pred_y = [] h0 = torch.zeros(size=(num_layers,1,hidden_size)) c0 = torch.zeros(size=(num_layers,1,hidden_size)) for i in X: out,(hn,cn) = lstm(torch.tensor(i,dtype=torch.float32).view(1,1,len(i)),h0,c0) h0,c0 = hn.detach(),cn.detach() pred_y.append(out.detach().flatten().item())
-
结果展示
8/2分进行train和test,训练结果如下:
全部train,预测下一日收盘价:(写这篇文章2022-3-15,预测3-16日股票收盘价)
2022-3-16:将预测结果与真实结果对比:
真实值与预测值之间有差异,但趋势对上了。
Date 预测值 真实值
2022-03-10 33303.748047 33174.07
2022-03-11 33237.870103 32944.19
2022-03-14 33265.761931 32945.24
2022-03-15 33353.923764 33544.34
2022-03-16 33751.452632 34063.10