The next step is to use the fit LSTM network to make forecasts.
下一步是用拟合的LSTM网络来做预测
A single forecast can be made with the fit LSTM network by calling model.predict(). Again, the data must be formatted into a 3D array with the format [samples, timesteps, features].
通过调用model.predict(),拟合的LSTM网络可以做单个预测,再次,数据必须被格式化为【samples,timesteps,features】的3D数组。
We can wrap this up into a function called forecast_lstm().
我们可以把这打包到一个名为forecast_lstm()的函数。
1
2
3
4
5
6
7
8
|
# 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
)
# convert to array
return
[
x
for
x
in
forecast
[
0
,
:
]
]
|
We can call this function from the make_forecasts() function and update it to accept the model as an argument. The updated version is listed below.
我们可以从make_forecasts()中调用这个函数,并将其更新为接受模型作为参数,下面列出了更新版本
1
2
3
4
5
6
7
8
9
10
|
# evaluate the persistence model
def
make_forecasts
(
model
,
n_batch
,
train
,
test
,
n_lag
,
n_seq
)
:
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
|
This updated version of the make_forecasts() function can be called as follows:
make_forecasts()函数的更新版本可以调用如下:
1
2
|
# make forecasts
forecasts
=
make_forecasts
(
model
,
1
,
train
,
test
,
1
,
3
)
|