Keras 在LSTM模型预测了测试集后,如何预测未来30天的股价?

Keras 在LSTM模型预测了测试集后,如何预测未来30天的股价?,keras,deep-learning,lstm,python-3.7,Keras,Deep Learning,Lstm,Python 3.7,我使用了一个包含特定股票收盘价的数据集5年了,它有1231天的收盘价。列车集包含987天,测试集包含最后244天。在预测测试集后,我得到了5-6的RMSE精度 问题陈述:在LSTM模型预测了最后244个值之后,如何继续预测未来30天的股价 数据集- #import packages import pandas as pd import numpy as np #to plot within notebook import matplotlib.pyplot as plt %matplotlib

我使用了一个包含特定股票收盘价的数据集5年了,它有1231天的收盘价。列车集包含987天,测试集包含最后244天。在预测测试集后,我得到了5-6的RMSE精度

问题陈述:在LSTM模型预测了最后244个值之后,如何继续预测未来30天的股价

数据集-

#import packages
import pandas as pd
import numpy as np

#to plot within notebook
import matplotlib.pyplot as plt
%matplotlib inline

#setting figure size
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 20,10

#for normalizing data
from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler(feature_range=(0, 1))

#read the file
df = pd.read_csv('TATAGLOBAL.NS.csv')
df=df.dropna()

#print the head
df.shape


#importing required libraries
from sklearn.preprocessing import MinMaxScaler
from keras.models import Sequential
from keras.layers import Dense, Dropout, LSTM

#setting index as date
df['Date'] = pd.to_datetime(df.Date,format='%Y-%m-%d')
df.index = df['Date']
print(df.head(6))
#plot
plt.figure(figsize=(16,8))
plt.plot(df['Close'], label='Close Price history')


data = df.sort_index(ascending=True, axis=0)

new_data = pd.DataFrame(index=range(0,len(df)),columns=['Date', 'Close'])
for i in range(0,len(data)):
    new_data['Date'][i] = data['Date'][i]
    new_data['Close'][i] = data['Close'][i]

#setting index
new_data.index = new_data.Date
new_data.drop('Date', axis=1, inplace=True)

#creating train and test sets
dataset = new_data.values

train = dataset[0:987,:]
valid = dataset[987:,:]
#converting dataset into x_train and y_train
scaler = MinMaxScaler(feature_range=(0, 1))
scaled_data = scaler.fit_transform(dataset)

x_train, y_train = [], []
for i in range(60,len(train)):
    x_train.append(scaled_data[i-60:i,0])
    y_train.append(scaled_data[i,0])
x_train, y_train = np.array(x_train), np.array(y_train)


x_train = np.reshape(x_train, (x_train.shape[0],x_train.shape[1],1))
print(x_train.shape)

# create and fit the LSTM network
model = Sequential()

model.add(LSTM(units=50, return_sequences=True, input_shape=(x_train.shape[1],1)))
model.add(LSTM(units=50))
model.add(Dense(30,activation='relu'))
model.add(Dense(1,activation='linear'))
model.compile(loss='mean_squared_error', optimizer='adam')
model.fit(x_train, y_train, epochs=5, batch_size=1, verbose=2)

#predicting 246 values, using past 60 from the train data
inputs = new_data[len(new_data) - len(valid) - 60:].values
print(len(inputs))
inputs = inputs.reshape(-1,1)
print(inputs.shape)
inputs  = scaler.fit_transform(inputs)

X_test = []
for i in range(60,inputs.shape[0]):
    X_test.append(inputs[i-60:i,0])
X_test = np.array(X_test)

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

rms=np.sqrt(np.mean(np.power((valid-closing_price),2)))
rms

train = new_data[:987]
valid = new_data[987:]
valid['Predictions'] = closing_price
plt.plot(train['Close'])
plt.plot(valid[['Close','Predictions']])