The final step is to evaluate the forecasts.
最后一步是评估预测。
We can do that by calculating the RMSE for each time step of the multi-step forecast, in this case giving us 3 RMSE scores. The function below, evaluate_forecasts(), calculates and prints the RMSE for each forecasted time step.
我们可以计算多步预测每一步的RMSE来实现这一点,在这个例子中,给出3个RMSE分数,下面的函数评估预测,为每一个预测时间步计算和打印RMSE。
1
2
3
4
5
6
7
|
# evaluate the RMSE for each forecast time step
def
evaluate_forecasts
(
test
,
forecasts
,
n_lag
,
n_seq
)
:
for
i
in
range
(
n_seq
)
:
actual
=
test
[
:
,
(
n_lag
+
i
)
]
predicted
=
[
forecast
[
i
]
for
forecast
in
forecasts
]
rmse
=
sqrt
(
mean_squared_error
(
actual
,
predicted
)
)
print
(
't+%d RMSE: %f'
%
(
(
i
+
1
)
,
rmse
)
)
|
We can call it as follows:
我们可以像下面这样调用它
1
|
evaluate_forecasts
(
test
,
forecasts
,
1
,
3
)
|
It is also helpful to plot the forecasts in the context of the original dataset to get an idea of how the RMSE scores relate to the problem in context.
在原始数据集的背景下绘制预测是有益的,可以了解RMSE分数是如何与上文中问题相关的
We can first plot the entire Shampoo dataset, then plot each forecast as a red line. The function plot_forecasts() below will create and show this plot.
我们可以先打印出来整个洗发水数据集,然后把每个预测作为一根红线绘制。 下面的函数plot_forecasts()将创建和显示该图。
1
2
3
4
5
6
7
8
9
10
11
12
|
# plot the forecasts in the context of the original dataset
def
plot_forecasts
(
series
,
forecasts
,
n_test
)
:
# plot the entire dataset in blue
pyplot
.
plot
(
series
.
values
)
# plot the forecasts in red
for
i
in
range
(
len
(
forecasts
)
)
:
off_s
=
len
(
series
)
-
n_test
+
i
off_e
=
off_s
+
len
(
forecasts
[
i
]
)
xaxis
=
[
x
for
x
in
range
(
off_s
,
off_e
)
]
pyplot
.
plot
(
xaxis
,
forecasts
[
i
]
,
color
=
'red'
)
# show the plot
pyplot
.
show
(
)
|
We can call the function as follows. Note that the number of observations held back on the test set is 12 for the 12 months, as opposed to 10 for the 10 supervised learning input/output patterns as was used above.
我们可以像下面这样调用这个函数,请注意,测试集中的观察次数在12个月内为12次,而上述10次监督式学习输入/输出模式则为10次。(特么的,意思明白但held back on该怎么翻译呀!)
1
2
|
# plot forecasts
plot_forecasts
(
series
,
forecasts
,
12
)
|
We can make the plot better by connecting the persisted forecast to the actual persisted value in the original dataset.
我们可以是绘制更好点,通过关联持续预测到原始数据集上的实际持续值。
This will require adding the last observed value to the front of the forecast. Below is an updated version of the plot_forecasts() function with this improvement.
这需要将上次观察值添加到预测的前面。 下面是具有这种改进的plot_forecasts()函数的更新版本。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
# plot the forecasts in the context of the original dataset
def
plot_forecasts
(
series
,
forecasts
,
n_test
)
:
# plot the entire dataset in blue
pyplot
.
plot
(
series
.
values
)
# plot the forecasts in red
for
i
in
range
(
len
(
forecasts
)
)
:
off_s
=
len
(
series
)
-
12
+
i
-
1
off_e
=
off_s
+
len
(
forecasts
[
i
]
)
+
1
xaxis
=
[
x
for
x
in
range
(
off_s
,
off_e
)
]
yaxis
=
[
series
.
values
[
off_s
]
]
+
forecasts
[
i
]
pyplot
.
plot
(
xaxis
,
yaxis
,
color
=
'red'
)
# show the plot
pyplot
.
show
(
)
|