Loading the problem
Before you start
Turn a series into a supervised learning dataset with sliding windows.
To train a regressor or a neural network to forecast, you need pairs of inputs
and targets. Each input is the last lookback values, and each target is the
horizon values that come right after them. Slide the window forward one step
at a time:
X[j] = series[j : j + lookback]
y[j] = series[j + lookback : j + lookback + horizon]
There are n - lookback - horizon + 1 windows for a series of length n.
def make_windows(series, lookback, horizon):
Input
make_windows(np.array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0]), lookback=3, horizon=1)
Output
X = array([[1., 2., 3.],
[2., 3., 4.],
[3., 4., 5.]])
y = array([[4.],
[5.],
[6.]])
Reasoning
The first window sees 1, 2, 3 and must predict 4. Each later window moves
one step. The last full window sees 3, 4, 5 and predicts 6. There are
6 - 3 - 1 + 1 = 3 windows.
(X, y) with shapes (m, lookback) and (m, horizon).0 rows and
the right number of columns.Check runs the visible cases against a fresh kernel. Submit runs the hidden ones too.
⇧↵run cell