Sub topic 1
From trained model to served predictions
Prerequisites
Assume you know basic machine learning terms such as training, features, models, and predictions. You should also know Python, scheduled jobs, and the basic idea of an HTTP API.
From trained model to served predictions
Training produces a model artifact: a saved representation of learned parameters and preprocessing steps. Serving is the process of using that artifact to produce predictions for new data. Inference means running the model on input features.
The main design choice is when inference happens:
- Batch prediction scores many records together on a schedule.
- Real-time prediction scores one request when it arrives.
Batch prediction: scoring many records on a schedule
A batch job reads a dataset, computes or loads its features, scores all rows, and writes the results to storage. A downstream application can read those results later.
import joblib
import pandas as pd
model = joblib.load("model.joblib")
customers = pd.read_parquet("customers.parquet")
columns = ["tenure_months", "monthly_spend", "support_tickets"]
customers["churn_score"] = model.predict_proba(customers[columns])[:, 1]
customers[["customer_id", "churn_score"]].to_parquet("scores.parquet")Batch prediction fits use cases such as daily customer-risk lists, nightly demand forecasts, and large reports. It usually achieves high throughput because the system processes many rows together. Its weakness is staleness: a result may be several hours old.
Real-time prediction: scoring one request on demand
An online service loads the model at startup. Each HTTP request supplies features, and the service returns a prediction immediately. This pattern fits fraud checks, recommendations, and interactive applications.
The service must validate input, keep feature order consistent, and return an explicit error when required data is missing.
Latency versus throughput
Latency is the time one request waits for a result. Throughput is the amount of work completed per unit of time. Real-time serving prioritizes low latency. Batch serving prioritizes throughput and efficient resource use.
| Concern | Batch | Real time |
|---|---|---|
| Response | Stored later | Immediate |
| Typical strength | High throughput | Low latency |
| Main risk | Stale results | Service or feature-store failure |
Exercise
Which approach suits a daily email listing customers likely to churn?
Show answer
Batch prediction, because the full list can be scored on a schedule and consumed later.