python - 如何使用LSTM模型进行多步预测?

标签 python tensorflow machine-learning keras lstm

我用 LSTM 开发了一个时间序列模型。我不能用它来预测 future 几天的股价。我想用它来预测明年的股价并绘制它。如何用它来预测 future (明年)的股价?

df=pd.read_csv('foolad.csv')
df=df.set_index(pd.DatetimeIndex(df['Date'].values))

data=df.filter(['Close'])
dataset=data.values

training_data_len=math.ceil(len(dataset)*0.8)
scaler=MinMaxScaler(feature_range=(0,1))
scaled_data=scaler.fit_transform(dataset)
scaled_data

training_data=scaled_data[0:training_data_len , :]

xtrain=[]
ytrain=[]
n = 60

for i in range(n,len(training_data)):
    xtrain.append(training_data[i-n:i , 0])
    ytrain.append(training_data[i,0])

xtrain , ytrain = np.array(xtrain) , np.array(ytrain)
xtrain=np.reshape(xtrain , (xtrain.shape[0],xtrain.shape[1],1))
xtrain.shape

model=Sequential()
model.add(LSTM(50,return_sequences=True,input_shape=(xtrain.shape[1],1)))
model.add(LSTM(50,return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))

model.compile(loss='mean_squared_error',optimizer='adam')

model.fit(xtrain,ytrain,epochs=1,batch_size=1)

test_data=scaled_data[training_data_len - n : , :]
xtest=[]
ytest=dataset[training_data_len : , :]
for i in range(n , len(test_data)):
    xtest.append(test_data[i-n : i , 0])

xtest=np.array(xtest)
xtest=np.reshape(xtest , (xtest.shape[0],xtest.shape[1],1))

prediction=model.predict(xtest)
prediction=scaler.inverse_transform(prediction)

#for future 360 days what can I do?....

最佳答案

一种方法是将预测作为输入反馈给模型:在每一步中,您都可以通过删除最旧的值并将最新的预测添加为最新值来更新输入序列。下面示意性地说明了这一点,其中 n 是输入序列的长度,T 是时间序列的长度。

enter image description here

下面的代码展示了如何为您的 LSTM 模型实现这种方法并绘制结果。

import numpy as np
import pandas as pd
import yfinance as yf
import tensorflow as tf
from tensorflow.keras.layers import Dense, LSTM
from tensorflow.keras.models import Sequential
from sklearn.preprocessing import MinMaxScaler

# download the data
df = yf.download(tickers=['^IXIC'], period='5y')
y = df['Close'].fillna(method='ffill').values.reshape(- 1, 1)

# scale the data
scaler = MinMaxScaler(feature_range=(0, 1))
scaler = scaler.fit(y)
y = scaler.transform(y)

# generate the training sequences
n_forecast = 1 
n_lookback = 60

X = []
Y = []

for i in range(n_lookback, len(y) - n_forecast + 1):
    X.append(y[i - n_lookback: i])
    Y.append(y[i: i + n_forecast])

X = np.array(X)
Y = np.array(Y)

# train the model
tf.random.set_seed(0)

model = Sequential()
model.add(LSTM(50, return_sequences=True, input_shape=(X.shape[1], X.shape[2])))
model.add(LSTM(50, return_sequences=False))
model.add(Dense(25))
model.add(Dense(1))

model.compile(loss='mse', optimizer='adam')
model.fit(X, Y, epochs=100, batch_size=128, validation_split=0.2, verbose=0)

# generate the multi-step forecasts
n_future = 365
y_future = []

x_pred = X[-1:, :, :]  # last observed input sequence
y_pred = Y[-1]         # last observed target value

for i in range(n_future):

    # feed the last forecast back to the model as an input
    x_pred = np.append(x_pred[:, 1:, :], y_pred.reshape(1, 1, 1), axis=1)

    # generate the next forecast
    y_pred = model.predict(x_pred)

    # save the forecast
    y_future.append(y_pred.flatten()[0])

# transform the forecasts back to the original scale
y_future = np.array(y_future).reshape(-1, 1)
y_future = scaler.inverse_transform(y_future)

# organize the results in a data frame
df_past = df[['Close']].reset_index()
df_past.rename(columns={'index': 'Date'}, inplace=True)
df_past['Date'] = pd.to_datetime(df_past['Date'])
df_past['Forecast'] = np.nan

df_future = pd.DataFrame(columns=['Date', 'Close', 'Forecast'])
df_future['Date'] = pd.date_range(start=df_past['Date'].iloc[-1] + pd.Timedelta(days=1), periods=n_future)
df_future['Forecast'] = y_future.flatten()
df_future['Close'] = np.nan

results = df_past.append(df_future).set_index('Date')

# plot the results
results.plot(title='NASDAQ')

enter image description here

参见this answer不同的方法。

关于python - 如何使用LSTM模型进行多步预测?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69785891/

相关文章:

python - music21:按音符位置将音符写入 MIDI 文件

python - Tensorflow训练,如何防止训练节点删除

tensorflow - 如何在 Tensorflow 2.0 + Keras 中进行并行 GPU 推理?

python - Tensorflow只保存检查点文件,不保存其他数据

python - Tensorflow 在使用队列的 sess.run 调用上卡住

python - 生产中是否需要 Python 虚拟环境?

python - 是什么让 Python 成为一种优秀的脚本语言?

python - 如何使用 TensorFlow 正确实现动态卷积神经网络

python - 使用 Python 的 Facebook API。如何打印出所有帖子的评论?

tensorflow - 为什么 Keras 模型仅使用 imagenet 权重进行实例化?