Skip to main content

Machine Learning in Production

A model in a notebook is not a product. Production ML adds reliability, monitoring and a feedback loop.

From notebook to system​

StagePractices
ReproducibilityFixed random seeds, pinned dependencies, versioned data and code
Training pipelineScripts or workflows, not manual notebook runs
PackagingSave the whole pipeline (preprocessing + model)
ServingAn API or batch job with input validation
MonitoringLatency, errors, input distribution, prediction quality
RetrainingScheduled or triggered by drift

Save and load the pipeline​

import joblib
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.ensemble import RandomForestClassifier

pipe = make_pipeline(StandardScaler(), RandomForestClassifier(random_state=0))
pipe.fit(X_train, y_train)
joblib.dump(pipe, "model-v1.joblib")

pipe = joblib.load("model-v1.joblib")
pipe.predict(X_test[:5])

Serve with an API​

from fastapi import FastAPI
from pydantic import BaseModel, Field
import joblib

app = FastAPI()
model = joblib.load("model-v1.joblib")

class Features(BaseModel):
age: float = Field(ge=0, le=120)
income: float = Field(ge=0)

@app.post("/predict")
def predict(f: Features):
pred = model.predict([[f.age, f.income]])[0]
return {"prediction": int(pred), "model_version": "v1"}

Monitoring and drift​

TypeMeaningDetection
Data driftInput distribution changesCompare feature statistics (PSI, KS test)
Concept driftRelationship between inputs and target changesTrack live accuracy when labels arrive
Data qualityMissing or out-of-range valuesSchema and range checks

Good practices​

  • Keep a baseline model and compare new models against it.
  • Log inputs, predictions and model version (respecting privacy).
  • Ship behind a feature flag or canary; keep rollback ready.
  • Document intended use, limitations and known biases (a "model card").
  • Train/serve skew: use the same preprocessing code in both.

See Reliability and observability for general operations practice.