Skip to content
Tutorials Center
My learning

Topics

Lesson 39 of 40 ยท Projects and Practice

Project: Time Series Forecasting

Forecast a seasonal time series: chronological splits, sliding windows, naive and linear baselines, normalization without leakage, then Keras Dense, LSTM and multi-step models.

Forecasting predicts future values of a series from its past: energy demand, temperature, sales, sensor readings. It looks like ordinary regression, but time adds traps: you must not shuffle across time, you must not let the future leak into the past, and a model must beat very simple baselines before it earns its complexity. This project walks through the full workflow with a seasonal series that you can run here, and the matching Keras code (read-only).

TensorFlow is not available in the browser runner. keras code is read-only; the NumPy blocks run here and use the same steps.

Step 1: a series and a chronological split

The series below mimics hourly temperature: a daily cycle (period 24), a slow trend and noise. Split by time: the first 70 percent for training, the next 15 for validation, the last 15 for testing. Shuffling before splitting would let the model train on points surrounding its test points and give a falsely good score.

Python Generate and split a series

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Step 2: normalize using training statistics only

Compute the mean and standard deviation on the training part and apply them to everything. Using statistics of the whole series would leak information from the future.

Python Normalization without leakage

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The test mean is not zero, because the series has a trend. That is expected and honest; scaling it with its own mean would hide the trend.

Step 3: baselines

Two simple forecasts to beat:

  • Naive: predict the last observed value.
  • Seasonal naive: predict the value from one period ago (24 hours earlier).

If a neural network cannot beat the seasonal naive forecast, it has learned nothing useful.

Python Naive and seasonal-naive baselines

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here the seasonal naive forecast wins, because the daily cycle is steep. On smoother series (slow-moving prices, say) the plain naive forecast is often hard to beat for one-step-ahead predictions, and the seasonal naive forecast takes over for longer horizons. Both copy one noisy value, so each carries that value's noise. Always report errors in the original units (degrees), not only in scaled units.

Step 4: sliding windows

A supervised model needs (inputs, target) pairs. Take a window of the last L values as input and the next value as target. Slide it one step at a time.

Python Windowed dataset

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Keras provides keras.utils.timeseries_dataset_from_array(data, targets, sequence_length, batch_size=...) that builds these windows lazily as a tf.data.Dataset. Create the windows within each split so training windows never overlap test data.

Step 5: a linear model (autoregression)

A linear model on the last 24 values is a strong, cheap model. It is also what a single Keras Dense(1) layer on the window computes.

Python Linear autoregression with least squares

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The linear model beats the naive forecast because it can average over noise and use the cycle. A neural network has to do better than this to be worth its cost.

Step 6: Keras models

Python
import keras
from keras import layers

window = 24
train_ds = keras.utils.timeseries_dataset_from_array(
    train_scaled[:-1], train_scaled[window:], sequence_length=window,
    batch_size=64, shuffle=True)
val_ds = keras.utils.timeseries_dataset_from_array(
    val_scaled[:-1], val_scaled[window:], sequence_length=window, batch_size=64)

# 1. linear: the Dense baseline
linear = keras.Sequential([
    keras.Input(shape=(window, 1)),
    layers.Flatten(),
    layers.Dense(1),
])

# 2. recurrent
lstm = keras.Sequential([
    keras.Input(shape=(window, 1)),
    layers.LSTM(32),
    layers.Dense(1),
])

for m in (linear, lstm):
    m.compile(optimizer=keras.optimizers.Adam(1e-3), loss="mse", metrics=["mae"])
    m.fit(train_ds, validation_data=val_ds, epochs=20,
          callbacks=[keras.callbacks.EarlyStopping(patience=3, restore_best_weights=True)])

Shuffling the windows inside the training set is fine (each window is self-contained); what you must never do is mix windows from different splits. Note shuffle=False for validation and test. If the series has more than one feature (temperature, pressure, humidity), the feature axis grows to (window, n_features); the target stays the one you want to predict.

Step 7: forecasting several steps ahead

Two common approaches:

  • Direct: make the model output H values at once (Dense(H)), trained on windows with a target vector of the next H steps. Errors do not compound.
  • Recursive: predict one step, append it to the window, repeat. Simple, but errors feed back into later predictions.
Python Recursive multi-step forecast

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Pitfalls checklist

  • Shuffling before the split, or random train/test splitting of windows from a single series.
  • Scaling with statistics from the whole series.
  • Not comparing with naive and seasonal baselines.
  • Evaluating only on the training period or a single lucky period; use a few time-ordered folds when you can (TimeSeriesSplit in scikit-learn).
  • Ignoring features you know in advance (hour of day, holiday): add them as inputs, for example as sin and cos of the hour.
  • Forecasting far ahead with a model trained only for one step.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How should a time series be split for evaluation?

    A real forecaster only has the past, so evaluation must respect time order.
  2. 2. What does a seasonal naive forecast with period 24 predict for hour t?

    It repeats the value from the same hour of the previous day.
  3. 3. Why compute normalization statistics on the training data only?

    The model must not use information that would not exist at prediction time.
  4. 4. A weakness of recursive multi-step forecasting is that:

    Each prediction becomes an input for the next, so early errors propagate.

Summary

  • Split chronologically, normalize with training statistics, and window within each split.
  • Beat the naive and seasonal-naive baselines first, then try a linear model, then LSTM or convolutional models.
  • Report errors in original units.
  • Multi-step forecasting is direct (output H values) or recursive (feed predictions back).
  • Add known calendar features and test across several time periods.

Next: Practice and Troubleshooting