Spot drift in the data a model sees

DevOps, Hands On20 minutesFreeSteps checked 23 November 2026By Cloud Hands On

In this task you'll notice when the data a model sees no longer looks like the data it learnt from.

A model only knows the world it was trained on. Data drift is when new inputs look different (longer deliveries, busier evenings). Concept drift is when the link between inputs and the answer changes (a new courier fixes one warehouse). Both can make a good model quietly wrong.

The population stability index (PSI) compares a feature's spread in training data and new data. Rules of thumb: under 0.1 stable, 0.1 to 0.25 watch it, over 0.25 a real change. drift.py checks each feature and exits with code 2 on drift, so a pipeline can react. Drift is a reason to retrain and compare, not proof the model is worse: only results on labelled new data show that.

Example. Harbourline Design's enquiry-scoring model was trained before a big marketing campaign. Afterwards most enquiries came from a new country, the PSI for that feature jumped to 0.6, and the model's scores stopped matching what sales saw. The drift check would have flagged it in the first week.

Before you start

You need: Python 3.10 or newer, and about 500 MB of disk for MLflow and scikit-learn. No cloud account. Time: 20 minutes.

Where to look

  1. mlops folder
  2. drift.py

Steps

  1. Save drift.py from Commands.
  2. Compare training data with today's orders: python drift.py data/orders.csv data/today.csv. Every feature is ok and the result is stable.Today's orders come from the same pattern as the training data.
  3. Make a busier, further-away month: python make_data.py --rows 1000 --seed 12 --drift 1 --out data/next_month.csv.
  4. Compare again with next_month.csv. distance_km, items and hour show DRIFT, and the exit code is 2.
  5. Read the result line: drift means retrain and compare, which is the next task.

Commands

# drift.py: compare new data with the training data, feature by feature, using the population stability index (PSI).
# Rule of thumb: under 0.1 stable, 0.1 to 0.25 watch it, over 0.25 the model is seeing a different world. Exit code 2 = drift.
import sys
import numpy as np, pandas as pd

def psi(expected, actual, bins=10):
    edges = np.unique(np.quantile(expected, np.linspace(0, 1, bins + 1)))
    edges[0], edges[-1] = -np.inf, np.inf
    e = np.histogram(expected, edges)[0] / len(expected)
    a = np.histogram(actual, edges)[0] / len(actual)
    e, a = np.clip(e, 1e-4, None), np.clip(a, 1e-4, None)
    return float(np.sum((a - e) * np.log(a / e)))

train = pd.read_csv(sys.argv[1] if len(sys.argv) > 1 else "data/orders.csv")
new = pd.read_csv(sys.argv[2] if len(sys.argv) > 2 else "data/today.csv")
worst = 0.0
for col in ["distance_km", "items", "hour", "weekend"]:
    v = psi(train[col], new[col]); worst = max(worst, v)
    print(f"{col:<12} PSI {v:6.3f}  {'DRIFT' if v > 0.25 else 'watch' if v > 0.1 else 'ok'}")
share = new["warehouse"].eq("south").mean() - train["warehouse"].eq("south").mean()
print(f"{'warehouse':<12} south share changed by {share:+.1%}")
print("RESULT:", "drift, retrain and compare before trusting the model" if worst > 0.25 else "watch" if worst > 0.1 else "stable")
sys.exit(2 if worst > 0.25 else 0)

# Stable, then drifted
python drift.py data/orders.csv data/today.csv
python make_data.py --rows 1000 --seed 12 --drift 1 --out data/next_month.csv
python drift.py data/orders.csv data/next_month.csv

Watch out for

Cost
Free.
Security
Drift reports can reveal business patterns; share them like other internal reports.
Common mistakes
Retraining automatically on every drift alarm without comparing models, and checking inputs but never real outcomes.

Clean up

Keep the mlops folder for the next tasks in this section; delete it (and its .venv) at the end.

Words to know

Data drift
New inputs look different from the training inputs.
Concept drift
The relationship between inputs and the answer has changed.
PSI
Population stability index: a number for how much a feature's spread has moved.
Labelled data
Records where the real outcome is now known.

Tick off each step and track your progress in the guide.

Open this task in DevOps, Hands On

Keep going