首页 > 解决方案 > Python 中的 LSTM 生成平面预测

问题描述

在此处输入图像描述在此处输入图像描述我有 2017 年 7 月至 2018 年 12 月的每日数据,这些数据本质上是非平稳的,我正在尝试生成未来六个月的预测,即;从 2019 年 1 月到 2019 年 7 月。我尝试使用 SARIMAX 和 LSTM,但我得到的预测持平。这是我第一次使用 LSTM,所以我尝试了 RELU 和 Sigmoid 作为激活函数,但预测结果平平

SARIMA
SARIMAX
LSTM

以下是一个月的数据:

           values
X_Date  
2017-07-01  15006.17
2017-07-02  15125.35
2017-07-03  13553.20
2017-07-04  14090.07
2017-07-05  14341.84
2017-07-06  15037.23
2017-07-07  15588.56
2017-07-08  16592.55
2017-07-09  16851.91
2017-07-10  15630.53
2017-07-11  15501.26
2017-07-12  15852.34
2017-07-13  15020.60
2017-07-14  17115.26
2017-07-15  17668.73
2017-07-16  17604.95
2017-07-17  16686.89
2017-07-18  16523.80
2017-07-19  17642.11
2017-07-20  17803.65
2017-07-21  18756.53
2017-07-22  19220.46
2017-07-23  18876.94
2017-07-24  18103.97
2017-07-25  18034.74
2017-07-26  16650.10
2017-07-27  17247.02
2017-07-28  17620.62
2017-07-29  18210.39
2017-07-30  17015.64
scaler = MinMaxScaler()
train = daily_data.iloc[:365]
test = daily_data.iloc[365:]

scaler.fit(train)


scaled_train = scaler.transform(train)
scaled_test = scaler.transform(test)

from keras.preprocessing.sequence import TimeseriesGenerator

scaled_train
# define generator
n_input = 7
n_features = 1
generator = TimeseriesGenerator(scaled_train, scaled_train,    
length=n_input, batch_size=1)


from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM


# define model
model = Sequential()
model.add(LSTM(200, activation='sigmoid', input_shape=(n_input,     
n_features)))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

model.summary()

# fit model
model.fit_generator(generator,epochs=25)

model.history.history.keys()

loss_per_epoch = model.history.history['loss']
plt.plot(range(len(loss_per_epoch)),loss_per_epoch)

first_eval_batch = scaled_train[-7:]
first_eval_batch = first_eval_batch.reshape((1,n_input,n_features))

model.predict(first_eval_batch)

test_predictions = []

first_eval_batch = scaled_train[-n_input:]
current_batch = first_eval_batch.reshape((1, n_input, n_features))


np.append(current_batch[:,1:,:],[[[99]]],axis=1)



test_predictions = []

first_eval_batch = scaled_train[-n_input:]
current_batch = first_eval_batch.reshape((1, n_input, n_features))

for i in range(len(test)):

 # get prediction 1 time stamp ahead ([0] is for grabbing just the  
 number instead of [array])
 current_pred = model.predict(current_batch)[0]

 # store prediction
 test_predictions.append(current_pred) 

 # update batch to now include prediction and drop first value
 current_batch = np.append(current_batch[:,1:,:],                        

 [[current_pred]],axis=1)

预测是一条平线。

标签: pythonkerastime-serieslstm

解决方案


几个问题:

  1. 18 个月的日常数据对于神经网络构建对未来的准确预测可能并不重要。
  2. 您的模型只有 1 个 LSTM 层,添加第二个以受益于它的“记忆”:
from keras.layers import Dropout

# define model
model = Sequential()
model.add(LSTM(50, activation='relu', input_shape=(n_input,n_features), return_sequences=True))
model.add(Dropout(.4))
model.add(LSTM(100, activation='relu', return_sequences=False))
model.add(Dropout(.4))
model.add(Dense(1))
model.compile(optimizer='adam', loss='mse')

如果你提供你的数据,我可以仔细看看。您是否尝试过使用该n_input变量?这可能会对您的模型产生影响。


推荐阅读