Skip to main content
Data ScienceAdvanced20 min readUpdated March 2025

Deploying Data Science Models

A model only creates value when it is used. Learn how to package a trained model, serve predictions through an API, and monitor it for data drift once it is in production.

From Notebook to Production

Deployment turns a trained model into a dependable service. The common patterns are:

  • Batch scoring — Run predictions on a schedule and write results to a table.
  • Real-time API — Serve predictions on demand over HTTP.
  • Embedded — Ship the model inside an application or device.

Saving the Model

Persist the entire pipeline (preprocessing plus model) so training and serving stay consistent. Record the library versions used:

python
import joblib
import sklearn

joblib.dump(
    {"pipeline": model, "sklearn_version": sklearn.__version__,
     "features": numeric + categorical},
    "model_v1.joblib",
)

artifact = joblib.load("model_v1.joblib")
pipeline = artifact["pipeline"]

Serving Predictions with an API

A small FastAPI service validates inputs and returns predictions:

python
from fastapi import FastAPI
from pydantic import BaseModel
import pandas as pd
import joblib

app = FastAPI()
artifact = joblib.load("model_v1.joblib")
pipeline = artifact["pipeline"]

class House(BaseModel):
    sqft: float
    bedrooms: int
    age: float
    neighborhood: str
    style: str

@app.post("/predict")
def predict(house: House):
    row = pd.DataFrame([house.model_dump()])
    price = float(pipeline.predict(row)[0])
    return {"predicted_price": round(price, 2), "model_version": "v1"}

Monitoring in Production

Model quality decays as the world changes. Track:

  • Data drift — Input distributions shifting away from training data.
  • Concept drift — The relationship between inputs and target changing.
  • Performance metrics — Accuracy or error once ground truth arrives.
  • Operational health — Latency, error rate and throughput.

A Simple Drift Check

The Kolmogorov-Smirnov test compares a live feature to its training distribution:

python
from scipy.stats import ks_2samp

def drift_report(train_df, live_df, columns, alpha=0.01):
    report = {}
    for col in columns:
        stat, p = ks_2samp(train_df[col], live_df[col])
        report[col] = {"ks_stat": round(stat, 3), "drifted": p < alpha}
    return report

print(drift_report(X_train, live_batch, ["sqft", "age"]))

Key Takeaways

  • Deploy the full pipeline, not just the estimator.
  • Validate API inputs and version every model artifact.
  • Monitor for drift and retrain on a defined trigger.
  • Deployment is the start of a model lifecycle, not the end.

Contact Us

Have a question or feedback? Fill out the form below or reach us directly at support@nvaitraining.com