Sub topic 3
Monitoring models in production
This chapter assumes you can train and evaluate a supervised model in Python, use Git from the command line, and understand that a web service runs on a server. A model that passes evaluation is not finished. Its inputs change, its users change, and its accuracy can fall without any code change. Monitoring is how a team notices this.
Monitoring models in production
Monitor three things: the service (latency, errors, uptime), the inputs (the feature distributions the model receives), and the outputs (predictions and, when labels arrive later, real accuracy). Log each prediction with its input features, model version, and timestamp so you can investigate later.
Detecting data drift and concept drift
- Data drift: the input distribution changes, while the true relationship between inputs and outputs stays the same.
- Concept drift: the relationship itself changes, so the same inputs now map to different correct answers.
Testing for data drift
The following script compares a feature's training distribution with production values using a two-sample Kolmogorov-Smirnov test.
import numpy as np
from scipy.stats import ks_2samp
rng = np.random.default_rng(0)
reference = rng.normal(loc=0.0, scale=1.0, size=5000)
live = rng.normal(loc=0.5, scale=1.0, size=5000)
result = ks_2samp(reference, live)
print("drift" if result.pvalue < 0.01 else "no drift", round(result.statistic, 3))Concept drift needs labels, so you detect it by tracking accuracy on recent labeled data.
When and how to retrain
Retrain on a schedule, when a drift test fires, or when labeled accuracy falls below an agreed threshold. Run retraining through the same pipeline used for the original model, so the new version is evaluated against the current production model before promotion.
Governance, lineage and compliance
Lineage records which data, code, and parameters produced each model version. Governance uses that record to answer audit questions: who approved a release, what data it trained on, and which predictions it made. Store these records in the model registry alongside approval status.
Who does what: roles in an ML team
| Role | Main responsibility |
|---|---|
| Data scientist | Builds and evaluates models |
| ML engineer | Builds pipelines, serving, and retraining automation |
| Data engineer | Owns data sources and validation |
| Operations or platform engineer | Runs infrastructure and monitoring |
| Product owner | Sets success metrics and approves releases |
Assessing MLOps maturity and choosing next steps
Rate your team on versioning, automated pipelines, automated release checks, production monitoring, and documented governance. Pick the weakest area that blocks you today. For most teams without monitoring, that is the next step.
Exercise
A feature's production distribution has shifted, but the model's accuracy on recent labeled data is unchanged. Should you retrain now?
Show answer
Not necessarily. The shift is data drift, but the model is still accurate. Log the shift, keep watching accuracy, and retrain if accuracy falls below the agreed threshold.