How to cut a series into sliding windows (Forecast, version 2)?
This is version 2 of the series that builds, one small step at a time, a program that forecasts future sales from past sales. Version 1 looked at the data. Version 2 makes the first move towards prediction, and it still contains no AI: it only rearranges numbers. To keep the output small enough to see at a glance, this version practises on the ten numbers 0 to 9 instead of the real sales series.
Why sliding windows?
A forecasting model learns one basic pattern: given these recent values, predict the next value. For example, with sales figures from 10 time periods, we can use the values from periods 1 to 4 to predict period 5, the values from periods 2 to 5 to predict period 6, the values from periods 3 to 6 to predict period 7, and so on.
This way of making training examples is called a sliding window. Take a short piece of the series, move one time period forward, take the next piece, and repeat. In each piece, every value except the last is what the model is given, and the last value is the answer it must learn to predict. With many such examples, the model learns how recent sales relate to the next day's sales.
How the program works
1. Load torch
import torch
PyTorch, imported as torch, is the library that later versions
will use to build and train the model. Version 2 borrows just one of its tools,
unfold, which does the cutting.
2. Make a tiny series
data = torch.arange(10)
print(data)
torch.arange(10) builds the whole numbers from 0 up to 9 — a stand-in for
a time series of ten days. print(data) shows it as one line, so you can check
the raw material before it gets cut.
3. Cut the windows
# the unfold method extracts sliding windows along dimension 0
# (the only dimension here)
# window length is 5, and the windows shift by 1 element
windows = data.unfold(0, 5, 1)
unfold takes three arguments. The first, 0, names the direction to
cut along — dimension 0, the only direction this one-row series has. The second,
5, is the window length: each piece holds five numbers. The
third, 1, is the step: the window slides forward one position
each time.
4. Show each window
for window in windows:
print(window.numpy())
The for loop takes the windows one at a time, and the indented line runs once for
each of them. window.numpy() converts the current window into a plain array so it
prints neatly.
With ten numbers, a window of five, we can create [0 1 2 3 4],
[1 2 3 4 5], [2 3 4 5 6] as shown below.
What you should see
tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
[0 1 2 3 4]
[1 2 3 4 5]
[2 3 4 5 6]
[3 4 5 6 7]
[4 5 6 7 8]
[5 6 7 8 9]
The first line is the raw series. If the first four numbers of a window are the given days and the last number is the day to predict, then the program has already manufactured six training examples out of one series of ten numbers.
The whole program
import torch
data = torch.arange(10)
print(data)
# the unfold method extracts sliding windows along dimension 0
# (the only dimension here)
# window length is 5, and the windows shift by 1 element
windows = data.unfold(0, 5, 1)
for window in windows:
print(window.numpy())
Running it on your own computer
Download the zip file and unzip it
to a folder. The zip holds the programs in this series so far, the
time_series.csv data file, and pyproject.toml. This note uses
time_02.py. Open the folder in Visual Studio Code, then type these three lines
in the terminal:
uv python pin 3.12.10
uv sync
uv run time_02.py
The first line records which Python version this folder uses, and the second installs every
package that pyproject.toml lists. The third line prints the series and its
windows in the terminal.
Those three lines are needed only once. The next time you want to run the program again,
type just uv run time_02.py.
If you have not set up uv yet, start with How to install Visual Studio Code? and then the uv note for Windows or Mac.
What comes next
The cutting works, but the three arguments are hard-coded and the pieces are not yet labelled as input and answer. Version 3 packages the cutting into a function whose window size you can choose.