Sub topic 2
Batch prediction: scoring many records on a schedule
What you should already know
This chapter assumes you know basic machine learning vocabulary (training, model, features, prediction). It also assumes you can read Python and understand scheduled jobs such as cron. The previous chapter covered how a trained model becomes a source of predictions. Here we look at the first serving pattern.
The working model
Batch prediction means scoring many records in one run, on a schedule, and storing the results for later use. The model does not wait for a user. A job starts at a set time, reads a table of records, scores all of them, and writes the scores to storage. Other systems read those stored scores when they need them.
A typical run has four steps:
- A scheduler starts the job, for example every night at 02:00.
- The job loads the saved model and the input records.
- The model scores every record in one pass.
- The job writes the scores to a file or table with a date in its name.
A worked example
The job below scores customers for churn risk. Churn means a customer stops using a service.
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")Three details matter here:
predict_probais called once on the whole table. Scoring many rows in one vectorized call is far faster than looping over rows one at a time.[:, 1]selects the probability of the positive class, which is churn.- The output holds only
customer_idand the score. Downstream systems join it back to other data by key.
The date in both file names ties each score file to the input snapshot that produced it. That makes a run easy to audit and to repeat.
What batch does well
- High throughput. The job processes millions of rows in one pass and can use all available hardware.
- Low cost. Compute runs only while the job runs. You can use cheap, interruptible machines.
- Simple operation. There is no always-on service. If a run fails, you fix the problem and run it again.
- Easy feature access. Features come from the same data warehouse tables used in training. This lowers the risk of training-serving skew, a mismatch between how features are computed in training and in serving.
- Easy checking. You can inspect, validate, and compare the whole output before anyone uses it.
What batch gives up
The main cost is staleness. A score is only as fresh as the data at the time of the run. If a customer files three support tickets at 09:00, the 02:00 score does not reflect them until the next run.
Batch also cannot score a record that did not exist at run time. A customer who signs up at noon has no score until the next job. Batch also wastes work when you score every record but only a small share are ever read.
Common errors
- Reordered columns. Passing features in a different order than in training gives wrong scores with no error message. Select columns explicitly, as the example does with
feature_columns. - Silent partial runs. A job that fails halfway can leave a half-written file. Write to a temporary name, then rename it when the write is complete.
- Overwriting yesterday's scores. Keep dated outputs so you can compare runs and roll back.
- Ignoring run time. If the job takes longer than the gap between runs, runs overlap. Track duration and set alerts.
Exercise
A churn model scores all customers nightly at 02:00. A customer cancels at 09:00 and calls support at 09:30. A retention tool reads the stored score at 10:00. Which score does the tool see, and what is the risk?
Show answer
The tool sees the score from the 02:00 run, which was computed before the cancellation and the support call. The risk is staleness: the score ignores recent events, so the tool may rank this customer as low risk when the real risk is high. Storing a timestamp with the score lets the tool detect and report how old it is.