Score a file of records in a batch
In this task you'll score a whole file of new records with the champion model and record which version did it.
Batch scoring runs the model over many records at once, on a schedule: tonight's orders, this week's invoices. It's the simplest way to serve a model: no server to keep running, easy to re-run, and the results can be checked before anyone uses them.
Always write the model version next to each prediction. When someone asks why an order was flagged, you can find the exact model, its run and its data.
Example. SS Kaiser scores the next day's orders every evening at 18:00 and sends the riskiest ones to the courier desk. Each row carries the model version, so the morning review can tell whether a bad call came from the new model or the old one.
Before you start
You need: Python 3.10 or newer, and about 500 MB of disk for MLflow and scikit-learn. No cloud account. Time: 15 minutes.
Where to look
- mlops folder
- batch_score.py
- predictions.csv
Steps
- Save
batch_score.pyfrom Commands. - Run
python batch_score.py data/today.csv predictions.csv. It prints how many orders were scored and how many flagged, with the champion's version number. - Open predictions.csv: each row has
late_risk(0 to 1) andmodel_version. - Move @champion to another version (as in the registry task), score again, and see the version column change.
Commands
# batch_score.py: score a whole file of orders with whichever model is @champion, and record which version did it.
import sys
import mlflow, pandas as pd
from mlflow import MlflowClient
NAME = "delivery-late"
src, out = (sys.argv[1] if len(sys.argv) > 1 else "data/today.csv"), (sys.argv[2] if len(sys.argv) > 2 else "predictions.csv")
mlflow.set_tracking_uri("sqlite:///mlflow.db")
version = MlflowClient().get_model_version_by_alias(NAME, "champion").version
model = mlflow.sklearn.load_model(f"models:/{NAME}@champion")
df = pd.read_csv(src)
df["late_risk"] = model.predict_proba(df.drop(columns="late", errors="ignore"))[:, 1].round(3)
df["model_version"] = version
df.to_csv(out, index=False)
high = (df["late_risk"] > 0.5).sum()
print(f"scored {len(df)} orders with version {version}; {high} flagged as likely late -> {out}")
# Score today's orders
python batch_score.py data/today.csv predictions.csv
Watch out for
- Cost
- Free here. In the cloud, batch jobs cost only while they run.
- Security
- Prediction files can be as sensitive as the input data; store them with the same care.
- Common mistakes
- Not recording the model version, and scoring with a model loaded from a local file instead of the registry alias.
Clean up
Keep the mlops folder for the next tasks in this section; delete it (and its .venv) at the end.
Words to know
- Batch scoring
- Running a model over a file or table of records in one go.
- Probability
- The model's estimate between 0 and 1 that something is true.
- Threshold
- The probability above which you act, here 0.5.
- Model version
- The registry version that made a prediction.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On