How to shorten the slicing with a colon (Forecast, version 5)?
This is version 5 of the forecasting series. The only changes from version 4 are two lines that use shorthand to produce the same features and targets.
A version that changes nothing visible is still worth writing, because the shorthand appears in almost every Python program you will read from here on, and often in other Python programs by other developers. Learning to decode it once saves confusion many times later.
The two ways of saying the same thing
Version 4 said, in full:
row_n = windows.shape[0]
col_n = windows.shape[1]
features = windows[0:row_n, 0:col_n-1]
targets = windows[0:row_n, col_n-1:col_n]
Version 5 says the same thing in shorthand:
features = windows[:, :-1]
targets = windows[:, -1:]
Every part of the long form is still there, just implied.
0:row_n meant every row, so it became a bare :.
0:col_n-1 meant all but the last column, so it became :-1.
And col_n-1:col_n meant only the last column, so it became -1:.
How to read a colon and a minus one
1. A bare colon means "everything"
windows[:, :-1]
Inside the square brackets there are two positions separated by a comma: rows first, columns second. The bare colon in the row position says take every row, so the 1,431 examples all stay.
2. Minus one counts from the end
windows[:, :-1]
windows[:, -1:]
A negative number counts backwards: -1 is the last item, -2 the one
before it. So :-1 means from the beginning up to, but not including, the last
column — the 29 known days. And -1: means from the last column to
the end — the single day being predicted.
3. The rest of the file is untouched
Reading the CSV, converting to a tensor, building the dataset and the loader, and printing the four counts and three examples — all of that is exactly as in version 4. Only the two slicing lines inside the function changed.
What you should see
Series length: 1460
Number of windows: 1431
Feature shape: torch.Size([1431, 29])
Target shape: torch.Size([1431, 1])
The same four numbers as before, and the same three example windows after them.
The whole program
The pieces above, put back together:
import torch
from torch.utils.data import TensorDataset, DataLoader, Subset
import pandas as pd
def create_sliding_windows_with_target(data, window_size, shift=1):
windows = data.unfold(0, window_size, shift)
features = windows[:, :-1] # All columns except the last
targets = windows[:, -1:] # Just the last column
return features, targets
df = pd.read_csv('time_series.csv')
series = df['series'].values
series_tensor = torch.tensor(series, dtype=torch.float32)
window_size = 30
features, targets = create_sliding_windows_with_target(series_tensor, window_size=window_size, shift=1)
# Create PyTorch Dataset and DataLoader
dataset = TensorDataset(features, targets)
batch_size = 32
train_loader = DataLoader(dataset, batch_size=batch_size, shuffle=True)
# Print some information about the dataset
print(f"Series length: {len(series)}")
print(f"Number of windows: {len(features)}")
print(f"Feature shape: {features.shape}") # Should be (num_windows, window_size-1)
print(f"Target shape: {targets.shape}") # Should be (num_windows, 1)
# Show a few examples
print("\nFirst few windows:")
for i in range(3):
print(f"\nWindow {i+1}:")
print(f"Features (previous {window_size-1} days): {[f'{x:.2f}' for x in features[i].numpy()]}")
print(f"Target (next day): {targets[i].item():.2f}")
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_05.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_05.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 same four counts and
three examples as version 4.
Those three lines are needed only once. The next time you want to run the program again,
type just uv run time_05.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 examples are ready and the code is tidy, but there is still no way to tell whether a model is learning or merely memorising. Version 6 divides the examples into a training part and a validation part, so that progress can be checked on days the model never trained on.