Sub topic 17
Monitoring, drift and retraining
This chapter assumes you can train a scikit-learn pipeline, score it on a test set, and call predict on new rows. Deployment is not the end of the workflow. The test score you reported describes the data you had at training time. The world keeps changing after that.
Why a deployed model degrades
A model learns patterns from past data. If new data differs from that past, accuracy falls, and often nothing raises an error. The pipeline keeps returning predictions that look normal.
Two kinds of change cause most problems:
- Data drift (also called covariate shift) means the distribution of the input features changes. For example, a new marketing campaign brings in younger customers.
- Concept drift means the relationship between inputs and the target changes. For example, customers with the same profile now behave differently because a competitor lowered its prices.
Failures in the data pipeline matter as much as drift. A column that suddenly arrives empty, or in different units, shows up as drift in your monitoring.
What to monitor
| Signal | What it tells you | Needs true labels? |
|---|---|---|
| Input feature distributions | Data drift or broken upstream data | No |
| Share of missing values per column | Pipeline failures | No |
| Prediction distribution | Shifts in model output | No |
| Metric such as F1 on recent data | Real performance | Yes |
Labels often arrive late. A loan default is known months after the prediction. Until then, input and prediction monitoring are your early warning.
Detecting drift in a feature
A simple check compares a feature in the training data against recent production data with a two-sample Kolmogorov-Smirnov test. It asks whether the two samples plausibly come from the same distribution.
import numpy as np
from scipy.stats import ks_2samp
rng = np.random.default_rng(0)
train_income = rng.normal(50_000, 10_000, size=2000)
recent_income = rng.normal(58_000, 10_000, size=500) # shifted mean
result = ks_2samp(train_income, recent_income)
print("statistic:", round(result.statistic, 3))
print("p-value:", result.pvalue)
if result.pvalue < 0.01:
print("Drift suspected in 'income'")With large samples, tiny and harmless differences give small p-values. Treat the statistic as a size of change, set thresholds from experience, and look at the feature before acting.
Retraining
Two common retraining policies are:
- Scheduled: retrain weekly or monthly. This is simple and predictable, but it can retrain when nothing has changed.
- Triggered: retrain when a drift or performance threshold is crossed. This is efficient, but it needs reliable monitoring.
Retraining follows the same rules as the first training. Split the data again, keep a test set the model has never seen, and compare the new model to the current one on that same test set before replacing it. Keep the old model version so you can roll back.
Common mistakes
- Monitoring only accuracy, then waiting months for labels.
- Alerting on every small drift, which trains the team to ignore alerts.
- Retraining automatically and deploying without comparing against the current model.
- Not logging the inputs and predictions, which makes later analysis impossible.
Exercise
A fraud model's input features look unchanged, but its recall on newly labeled cases has dropped sharply. Which kind of drift is this most likely, and why can't input monitoring catch it?
Show answer
It is most likely concept drift. The relationship between the features and the target changed, for example because fraudsters adopted new tactics that look like normal behavior. The input distributions stay the same, so a check on features alone sees nothing. Only monitoring a metric against true labels reveals it.
Exercise
Why should a retrained model be compared with the current model on the same held-out test set before deployment?
Show answer
Retraining on new data does not guarantee a better model. The comparison on identical, unseen data shows whether the new version actually improves performance, and it prevents deploying a worse model by mistake.