LSTM
Time Series
Python
Forecasting
Machine Learning

Forecast future values with LSTM in Python

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

LSTMs are often used for sequence forecasting because they can learn patterns across time windows instead of treating each row as independent. The practical workflow is straightforward: turn the series into input windows, train on past-to-next-step examples, then roll the model forward to predict future values.

Prepare the Series as Sliding Windows

An LSTM expects a 3D tensor in the shape samples, timesteps, features. For a univariate time series, features is usually 1.

The function below turns one numeric series into training windows.

python
1import numpy as np
2
3
4def make_dataset(series: np.ndarray, window: int):
5    X = []
6    y = []
7    for start in range(len(series) - window):
8        X.append(series[start:start + window])
9        y.append(series[start + window])
10    X = np.array(X, dtype=np.float32)[..., np.newaxis]
11    y = np.array(y, dtype=np.float32)
12    return X, y
13
14
15series = np.sin(np.linspace(0, 20, 300)).astype(np.float32)
16X, y = make_dataset(series, window=20)
17print(X.shape)
18print(y.shape)

This creates examples such as "given the last 20 values, predict the next one." That one-step setup is often the easiest place to start.

Train a Simple LSTM Model

A small Keras model is enough to demonstrate the pattern.

python
1import numpy as np
2import tensorflow as tf
3from tensorflow import keras
4
5series = np.sin(np.linspace(0, 20, 300)).astype(np.float32)
6window = 20
7
8X = []
9y = []
10for start in range(len(series) - window):
11    X.append(series[start:start + window])
12    y.append(series[start + window])
13
14X = np.array(X, dtype=np.float32)[..., np.newaxis]
15y = np.array(y, dtype=np.float32)
16
17split = int(len(X) * 0.8)
18X_train, X_test = X[:split], X[split:]
19y_train, y_test = y[:split], y[split:]
20
21model = keras.Sequential([
22    keras.layers.Input(shape=(window, 1)),
23    keras.layers.LSTM(32),
24    keras.layers.Dense(1)
25])
26
27model.compile(optimizer="adam", loss="mse")
28model.fit(X_train, y_train, epochs=10, batch_size=16, validation_data=(X_test, y_test), verbose=1)

This is a one-step forecasting model. It learns to predict the next value after each input window.

Forecast Multiple Future Steps

To forecast beyond the next step, take the most recent window, predict one value, append that prediction, and slide the window forward. This is often called recursive forecasting.

python
1import numpy as np
2
3
4def forecast_future(model, history: np.ndarray, window: int, steps: int):
5    current = history[-window:].astype(np.float32).copy()
6    predictions = []
7
8    for _ in range(steps):
9        x = current.reshape(1, window, 1)
10        next_value = model.predict(x, verbose=0)[0, 0]
11        predictions.append(float(next_value))
12        current = np.append(current[1:], next_value)
13
14    return predictions
15
16
17future = forecast_future(model, series, window=20, steps=5)
18print(future)

This is simple and useful, but remember the tradeoff: prediction errors can accumulate because each new forecast becomes part of the next input.

Data Preparation Matters More Than Architecture Tweaks

LSTM examples often overemphasize layer choices and underemphasize data preparation. In real forecasting work, the following decisions matter at least as much as the network depth:

  • use chronological train-test splits, not random shuffles
  • scale the series if the magnitude changes widely
  • choose a window size that matches the problem's memory horizon
  • compare against simple baselines such as "predict the previous value"

If a naive baseline beats the LSTM, the problem is probably in the framing, features, or training setup rather than the lack of a more complex network.

When LSTM Is and Is Not a Good Fit

LSTMs can work well when the target depends on temporal context and you have enough sequential data to learn stable patterns. They are less compelling when the series is short, mostly linear, or dominated by strong seasonal structure that simpler models already capture.

That is why a good workflow starts with a small baseline and only then moves to recurrent models.

Common Pitfalls

Randomly shuffling time-series examples is a frequent mistake because it leaks future structure into training.

Using too short a window can hide important history. Using too long a window can make the model harder to train without adding useful information.

Forecasting many steps recursively without understanding error accumulation is another common problem. Multi-step prediction looks impressive, but the uncertainty grows quickly.

Finally, do not skip a baseline. If a simple persistence forecast performs almost as well as the LSTM, the extra model complexity may not be justified.

Summary

  • convert the series into sliding windows with shape samples, timesteps, features
  • train the LSTM to predict the next value from each window
  • for multi-step forecasting, roll the model forward recursively one prediction at a time
  • preserve chronological order in train-test splits
  • always compare LSTM results against simple forecasting baselines before trusting the added complexity

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.