Make Forecasts/进行预测
The next step is to make persistence forecasts.
下一步是进行持续预测
We can implement the persistence forecast easily in a function named persistence() that takes the last observation and the number of forecast steps to persist. This function returns an array containing the forecast.
我们可以用一个叫persistence()的函数来容易的实现持续预测,他接受最后一个观测值和预测步数来持续(预测)。
1
2
3
|
# make a persistence forecast
def
persistence
(
last_ob
,
n_seq
)
:
return
[
last_ob
for
i
in
range
(
n_seq
)
]
|
We can then call this function for each time step in the test dataset from December in year 2 to September in year 3.
我们可以为从第二年12月到第三年9月的测试数据集中的每一个时间步调用这个函数
Below is a function make_forecasts() that does this and takes the train, test, and configuration for the dataset as arguments and returns a list of forecasts.
下面是一个函数 make_forecasts(),它来执行此操作,并且它接受train,test,和 configuration for the dataset 作为参数,并且返回一个预测列表
1
2
3
4
5
6
7
8
9
10
|
# evaluate the persistence model
def
make_forecasts
(
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
=
persistence
(
X
[
-
1
]
,
n_seq
)
# store the forecast
forecasts
.
append
(
forecast
)
return
forecasts
|
We can call this function as follows:
我们可以像下面这样调用这个函数:
1
|
forecasts
=
make_forecasts
(
train
,
test
,
1
,
3
)
|