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
| Stage | Practices |
|---|---|
| Reproducibility | Fixed random seeds, pinned dependencies, versioned data and code |
| Training pipeline | Scripts or workflows, not manual notebook runs |
| Packaging | Save the whole pipeline (preprocessing + model) |
| Serving | An API or batch job with input validation |
| Monitoring | Latency, errors, input distribution, prediction quality |
| Retraining | Scheduled 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
| Type | Meaning | Detection |
|---|---|---|
| Data drift | Input distribution changes | Compare feature statistics (PSI, KS test) |
| Concept drift | Relationship between inputs and target changes | Track live accuracy when labels arrive |
| Data quality | Missing or out-of-range values | Schema 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.