Sub topic 1
From trained model to served predictions
Prerequisites
This chapter assumes you know basic machine learning vocabulary (training, model, features, prediction). It also assumes you can read Python and have a rough idea of what an HTTP API and a scheduled job are. No serving tools are needed yet.
What inference means
Training produces a model: a file of learned parameters plus the code that knows how to apply them. Inference is the act of applying that model to new data to get predictions. Serving is everything around inference that gets predictions to the people and systems that need them.
A model that sits in a notebook creates no value. Value appears when a prediction reaches a decision, such as which customers to call or whether to approve a payment. The main design question is how and when those predictions are produced.
The model as an artifact
Serving starts with saving the trained model so another process can load it. For scikit-learn models, joblib is a common choice.
import joblib
from sklearn.linear_model import LogisticRegression
X = [[12, 40.0, 0], [3, 95.5, 4], [30, 20.0, 1], [2, 80.0, 5]]
y = [0, 1, 0, 1]
model = LogisticRegression().fit(X, y)
joblib.dump(model, "model.joblib")
loaded = joblib.load("model.joblib")
print(loaded.predict_proba([[6, 70.0, 3]])[0, 1])The loaded model gives the same answers as the original. Every serving pattern in this tutorial starts from this point: load an artifact, pass it features, and return scores.
Two ways to run inference
The two patterns differ in when inference runs and how many records it handles at once.
- Batch prediction scores many records in one run, usually on a schedule. Results are stored and read later.
- Real-time (online) prediction scores one request at the moment it arrives and returns the answer right away.
Here is the batch form from the plan. A job loads the model, scores every customer row in one pass, and writes the results to a file that downstream systems read later.
import joblib
import pandas as pd
model = joblib.load("model.joblib")
customers = pd.read_parquet("customers_2026-11-28.parquet")
feature_columns = ["tenure_months", "monthly_spend", "support_tickets"]
customers["churn_score"] = model.predict_proba(customers[feature_columns])[:, 1]
customers[["customer_id", "churn_score"]].to_parquet("scores_2026-11-28.parquet")Here is the real-time form. A service loads the model once at startup and scores one request per call.
import joblib
from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
model = joblib.load("model.joblib")
class Features(BaseModel):
tenure_months: float
monthly_spend: float
support_tickets: int
@app.post("/predict")
def predict(features: Features):
row = [[features.tenure_months, features.monthly_spend, features.support_tickets]]
return {"churn_score": float(model.predict_proba(row)[0, 1])}Both snippets call the same predict_proba on the same model. What changes is the wrapper: a job that reads and writes files, or a service that answers requests.
Where the two paths differ
The table summarizes the contrast. Later chapters take each row in turn.
| Aspect | Batch | Real time |
|---|---|---|
| Trigger | Schedule or event | Incoming request |
| Unit of work | Many records | One record |
| Result delivery | Stored, read later | Returned immediately |
| Typical concern | Throughput | Response time |
Common errors at this stage
- Mismatched feature order. The model sees numbers, not column names. In the service above, the row must list features in the same order as training. A swapped order gives wrong scores and no error.
- Loading the model on every request. Load it once at startup, as the service does. Reloading per call adds delay for no benefit.
- Treating the model file as the whole system. The features, the schedule or endpoint, and the storage for results are part of serving too.
Exercise
In the batch script, what would you change to score a new day's customers, and why does the real-time service not need that change?
Show answer
Change the input and output file names (for example to the new date), usually by passing the date as a parameter, because the batch job reads a fixed snapshot of rows. The real-time service has no input file. It scores whatever features arrive in each request, so the date is irrelevant to it.