Python时间序列LSTM预测系列教程(11)-多步预测Multi-Step LSTM预测(2)
关联教程:
x factor
多步预测的LSTM⽹络
数据准备
1、变成具有稳定性数据greg smith
2、缩放数据
LSTM模型预测过程
1、数据预测处理,准备数据
2、定义模型
3、训练模型
4、预测
5、数据逆变换
6、评估
代码
from pandas import DataFrame
from pandas import Series
from pandas import concat
kiefrom pandas import read_csvpep英语六年级上册
from pandas import datetime
ics import mean_squared_error
from sklearn.preprocessing import MinMaxScaler
dels import Sequential
from keras.layers import Den
from keras.layers import LSTM
from math import sqrt
from matplotlib import pyplot
from numpy import array
# date-time parsing function for loading the datat
def parr(x):
return datetime.strptime('190'+x, '%Y-%m')
# convert time ries into supervid learning problem
def ries_to_supervid(data, n_in=1, n_out=1, dropnan=True):
n_vars = 1 if type(data) is list el data.shape[1]
df = DataFrame(data)buzzer
cols, names = list(), list()
# input quence (t-n, ... t-1)
for i in range(n_in, 0, -1):
cols.append(df.shift(i))
cols.append(df.shift(i))
names += [('var%d(t-%d)' % (j+1, i)) for j in range(n_vars)]
# forecast quence (t, t+1, ... t+n)
for i in range(0, n_out):
cols.append(df.shift(-i))
if i == 0:
names += [('var%d(t)' % (j+1)) for j in range(n_vars)]
el:
names += [('var%d(t+%d)' % (j+1, i)) for j in range(n_vars)]
# put it all together
agg = concat(cols, axis=1)
# drop rows with NaN values
if dropnan:
townhouagg.dropna(inplace=True)
return agg
# create a differenced ries
def difference(datat, interval=1):
diff = list()
for i in range(interval, len(datat)):
value = datat[i] - datat[i - interval]
diff.append(value)
return Series(diff)
# transform ries into train and test ts for supervid learning
def prepare_data(ries, n_test, n_lag, n_q):
# extract raw values
raw_values = ries.values
# transform data to be stationary
diff_ries = difference(raw_values, 1)
diff_values = diff_ries.values
diff_values = shape(len(diff_values), 1)
# rescale values to -1, 1
scaler = MinMaxScaler(feature_range=(-1, 1))
scaled_values = scaler.fit_transform(diff_values)
scaled_values = shape(len(scaled_values), 1)
# transform into supervid learning problem X, y
supervid = ries_to_supervid(scaled_values, n_lag, n_q)
supervid_values = supervid.values
# split into train and test ts
train, test = supervid_values[0:-n_test], supervid_values[-n_test:]
return scaler, train, test
# fit an LSTM network to training data
def fit_lstm(train, n_lag, n_q, n_batch, nb_epoch, n_neurons):
# reshape training into [samples, timesteps, features]
X, y = train[:, 0:n_lag], train[:, n_lag:]
英语单词X = X.reshape(X.shape[0], 1, X.shape[1])
# design network
model = Sequential()
model.add(LSTM(n_neurons, batch_input_shape=(n_batch, X.shape[1], X.shape[2]), stateful=True)) model.add(Den(y.shape[1]))
# fit network
for i in range(nb_epoch):
model.fit(X, y, epochs=1, batch_size=n_batch, verbo=0, shuffle=Fal)
<_states()
return model
# make one forecast with an LSTM,
def forecast_lstm(model, X, n_batch):
# reshape input pattern to [samples, timesteps, features]
X = X.reshape(1, 1, len(X))
# make forecast
forecast = model.predict(X, batch_size=n_batch)
forecast = model.predict(X, batch_size=n_batch)
# convert to array
return [x for x in forecast[0, :]]
# evaluate the persistence model
def make_forecasts(model, n_batch, train, test, n_lag, n_q): forecasts = list()
for i in range(len(test)):
X, y = test[i, 0:n_lag], test[i, n_lag:]
# make forecast
forecast = forecast_lstm(model, X, n_batch)
# store the forecast
forecasts.append(forecast)
return forecasts
# invert differenced forecast
def inver_difference(last_ob, forecast):
# invert first forecast
inverted = list()
inverted.append(forecast[0] + last_ob)
# propagate difference forecast using inverted first value
for i in range(1, len(forecast)):
inverted.append(forecast[i] + inverted[i-1])
return inverted
# inver data transform on forecasts
def inver_transform(ries, forecasts, scaler, n_test):
inverted = list()
for i in range(len(forecasts)):
# create array from forecast
forecast = array(forecasts[i])
forecast = shape(1, len(forecast))
# invert scaling
inv_scale = scaler.inver_transform(forecast)
inv_scale = inv_scale[0, :]
# invert differencing
index = len(ries) - n_test + i - 1字母表
last_ob = ries.values[index]
inv_diff = inver_difference(last_ob, inv_scale)
# store
inverted.append(inv_diff)
return inverted
# evaluate the RMSE for each forecast time step
def evaluate_forecasts(test, forecasts, n_lag, n_q):
for i in range(n_q):
actual = [row[i] for row in test]
predicted = [forecast[i] for forecast in forecasts]
rm = sqrt(mean_squared_error(actual, predicted))
print('t+%d RMSE: %f' % ((i+1), rm))
# plot the forecasts in the context of the original datat
def plot_forecasts(ries, forecasts, n_test):
# plot the entire datat in blue
pyplot.plot(ries.values)
# plot the forecasts in red
for i in range(len(forecasts)):
off_s = len(ries) - n_test + i - 1
off_e = off_s + len(forecasts[i]) + 1
xaxis = [x for x in range(off_s, off_e)]
yaxis = [ries.values[off_s]] + forecasts[i]
pyplot.plot(xaxis, yaxis, color='red')
# show the plot
pyplot.show()
# load datat
# load datat
ries = read_csv('shampoo-sales.csv', header=0, par_dates=[0], index_col=0, squeeze=True, date_parr=parr) # configure
n_lag = 1
n_q = 3
n_test = 10
n_epochs = 1500
n_batch = 1
n_neurons = 1
# prepare data
scaler, train, test = prepare_data(ries, n_test, n_lag, n_q)
# fit model
model = fit_lstm(train, n_lag, n_q, n_batch, n_epochs, n_neurons)pudong airport
# make forecasts
forecasts = make_forecasts(model, n_batch, train, test, n_lag, n_q)
# inver transform forecasts and test
forecasts = inver_transform(ries, forecasts, scaler, n_test+2)
actual = [row[n_lag:] for row in test]
actual = inver_transform(ries, actual, scaler, n_test+2)
# evaluate forecasts
evaluate_forecasts(actual, forecasts, n_lag, n_q)
# plot forecasts
plot_forecasts(ries, forecasts, n_test+2)
>bbr