☰ Machine Learning Tutorial Menu

Deploying and Monitoring Models (MLOps basics)

Written by CSA mentors · Updated 26 Sept 2026 · 5 min read


The first model I deployed for a client lived on my own laptop, scored a CSV every Monday, and stopped the week I went on leave. Nobody noticed for a month. A model in a notebook creates no value; it creates value when a retention agent sees a score in her CRM at 8 a.m., or a payment is checked for fraud in 200 milliseconds, and keeps creating value after the person who built it has moved on. Getting there and staying there is called MLOps. You don't need Kubernetes. You need a repeatable pipeline, versioning, a serving choice that fits the decision, and monitoring with a retraining plan.

From notebook to pipeline

Pipeline from data source to training job to model registry to serving API to app, with monitoring feeding a retrain trigger
StageMinimum viable versionWhat to store
Training jobone Python script, run by cron or a schedulercode version (git hash), data snapshot or query + date, random seed
Model registrya folder or MLflow with model files named by versionmodel file, metrics on validation and test, feature list, training date
Servingbatch CSV to the database, or a small FastAPI endpointrequest logs with inputs, outputs and model version
Monitoringa weekly SQL report and a dashboardinput distributions, prediction distribution, live accuracy when labels arrive

The one rule we'd tattoo on every graduate is that the same preprocessing code runs in training and serving. A scikit-learn Pipeline saved as one object (with joblib) guarantees the scaler, encoder and model travel together. Most "the model works in the notebook but not in production" tickets we've debugged were a scaler fitted on different data.

Batch or real time?

Batch scoringReal-time API
Hownightly job writes scores for all customers to a tableapp calls POST /predict per event
Latencyhoursmilliseconds
Fitschurn lists, demand forecasts, credit reviewsfraud at payment time, dynamic pricing, live recommendations
Complexitylow; a SQL table is the interfacehigher: uptime, scaling, feature freshness

Start with batch. Move to real time only when the decision happens inside a live transaction, and be honest that the second option roughly triples the engineering.

Why models rot: drift

Live F1 declining over months until it crosses an alert line, then recovering after retraining; panel explains data drift and concept drift
  • Data drift: the inputs change shape. Inflation doubles basket sizes; a new city launches; a form field is renamed and arrives empty.
  • Concept drift: the relationship changes. Fraudsters adapt; a competitor's promotion changes who churns.
  • Label delay: you often learn the truth weeks later (a loan defaults after 90 days), so accuracy monitoring lags and input monitoring must fill the gap.

Catch input drift with PSI

The Population Stability Index compares the distribution of a feature at training time with its distribution today. For each bin, take (actual% - expected%) x ln(actual% / expected%). The feature here is transaction amount, in three bins.

BinTraining share (expected)This month (actual)Differenceln(actual / expected)Contribution
under 5,0000.500.40-0.10ln(0.8) = -0.2230.0223
5,000 to 20,0000.300.300.0000.0000
over 20,0000.200.30+0.10ln(1.5) = 0.4050.0405
PSI0.063

The usual rule of thumb is that PSI under 0.10 is stable, 0.10 to 0.25 is worth a look, and above 0.25 the feature has shifted enough that retraining is probably due. Here 0.063 is a mild drift toward larger amounts, consistent with inflation. Log it and watch it. Run PSI weekly on every feature and on the prediction scores themselves, because the scores drifting is often the first sign anyone gets.

Retraining and rollback

  • Retrain on a schedule (monthly) or on a trigger (PSI above 0.25, live F1 below a floor).
  • Always compare the candidate against the current model on the same recent window before promoting it.
  • Keep the previous version deployable; a one-line rollback beats a midnight debugging session.
  • Shadow mode (new model scores but does not act) or a small A/B slice de-risks the switch.
# run locally: save a full pipeline and serve it with FastAPI (pip install fastapi uvicorn joblib)
import joblib
joblib.dump(model_pipeline, "churn_v1.3.joblib")

# serve.py
from fastapi import FastAPI
import joblib, pandas as pd
app = FastAPI()
model = joblib.load("churn_v1.3.joblib")

@app.post("/predict")
def predict(row: dict):
    X = pd.DataFrame([row])
    p = float(model.predict_proba(X)[0, 1])
    return {"model_version": "1.3", "churn_probability": round(p, 4)}
# run: uvicorn serve:app --port 8000

"All the alerts are boring now": A Karachi bank deployed a card-fraud model in January. By June the investigators complained that alerts were all the same dull cases. Monitoring showed live precision had dropped from 12% to 6% while PSI on "merchant category" sat at 0.31. A wave of new online merchants had appeared that didn't exist in the training data, and the fraudsters had moved to them. The fix was a monthly retraining job on a rolling 12-month window, PSI alerts per feature, and a rule that a new model must beat the old one on the latest four weeks before promotion. The team also added a "days since model trained" tile to the dashboard, which turned out to be the single number managers looked at most.

If you remember one thing

Deploy the whole pipeline, preprocessing plus model, as one versioned artifact, and version code, data snapshot and model together so any prediction can be reproduced. Start with batch scoring and go real-time only for in-transaction decisions. Monitor the inputs (PSI), the outputs, and live accuracy once labels arrive, because every model decays. Retrain on a schedule or a trigger, compare before you promote, and keep the previous version one command away.

Homework

  1. Compute PSI for a feature with training shares 0.60 / 0.40 and current shares 0.45 / 0.55. Is it stable?
  2. For each, choose batch or real time and justify in one sentence: weekly churn list for a telco; fraud check inside a JazzCash transfer; monthly credit-limit review; product recommendations on a Daraz product page.
  3. A model's live F1 has dropped, but every feature's PSI is under 0.05. Which kind of drift is this most likely to be, and what would you check?

Lesson 17 of 18

Sign in to track your progress and earn learning points for every lesson you finish.