Menu

Model Deployment

Model Deployment

A model brings value only when people or systems can use it. Deployment is the step that puts your model to work. This topic explains the options and walks you through serving a model as a simple web service.

Deployment Options and Strategies

There are several ways to deploy a model. In batch deployment, the model runs on a schedule, such as every night, and saves predictions to a database. This suits tasks like weekly sales forecasts.

In real-time deployment, the model sits behind an API and answers requests instantly. This suits uses like fraud checks or product recommendations. Some models also run directly on devices, such as phones.

Choose based on how fast predictions are needed, how much data there is, and what your team can maintain. Batch is simpler. Real-time needs more care for speed and reliability.

Saving and Loading Models

Before deploying, you must save the trained model. Joblib and pickle are common choices for Scikit-learn models. Other tools have their own formats, such as ONNX for sharing models across frameworks.

import joblib
joblib.dump(model, "churn_model.joblib")
loaded = joblib.load("churn_model.joblib")
print(loaded.predict(X_test[:5]))

Always test the loaded model on a few examples to confirm it behaves as expected. Save the preprocessing steps together with the model, ideally in one pipeline.

Never load model files from untrusted sources. Pickle files can run harmful code when loaded, so treat them with the same care as any program.

Serving a Model with Flask or FastAPI

An API lets other applications send data and get predictions back. Flask and FastAPI are two popular Python tools for this. FastAPI is newer, fast, and creates documentation automatically.

from fastapi import FastAPI
import joblib
app = FastAPI()
model = joblib.load("churn_model.joblib")
@app.post("/predict")
def predict(features: dict):
X = pd.DataFrame([features])
return {"prediction": int(model.predict(X)[0])}

Run it with uvicorn main:app and send a request to the /predict endpoint. You get a prediction in return, usually in JSON format.

Add input validation so the API rejects bad data with a clear message. Also add logging so you can see what requests arrive and how the model responds.

Containerizing with Docker

Docker packages your application and everything it needs into a container. This ensures it runs the same way on your laptop, on a server, or in the cloud. It solves the problem of "it works on my machine."

A Dockerfile describes how to build the container. It lists the base image, the files to copy, the libraries to install, and the command to run.

FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

Build it with docker build and run it with docker run. Once it works locally, you can deploy the same container to cloud services such as AWS, Azure, or Google Cloud.


Data Science Lifecycle Tutorial

WR

Wesline Rinisha



Learn via Video Course

undefined Logo

NaN hrs



Get in Touch For Details