Sub topic 8
Evaluating Models and Setting Release Criteria
This chapter assumes you can train and score a supervised model with scikit-learn and read basic Python. You should also know the lifecycle stages from earlier chapters. Here you will turn "the model looks good" into a written, automated decision.
Why evaluation is a release decision
Evaluation answers one question: should this model replace what is running now? A single accuracy number on a test set rarely answers it. Release criteria are explicit rules, agreed before training, that a candidate model must satisfy before it moves forward in the registry.
Writing the rules down has three benefits:
- The decision is the same no matter who trains the model.
- A CI/CD step can enforce the decision without a human reading a notebook.
- You can audit later why a model was released.
The working model: three kinds of checks
A release gate normally combines three kinds of checks.
| Check | Question it answers | Example |
|---|---|---|
| Absolute floor | Is the model good enough to be useful at all? | Accuracy at least 0.90 |
| Relative comparison | Is it at least as good as production? | Not worse than the current model |
| Slice and safety checks | Does it fail badly for any group or case? | Recall per class or region above a minimum |
Add operational checks too, such as prediction latency and model size. A model that is more accurate but too slow for your service is not a valid release.
Choose data for evaluation carefully
Most evaluation errors come from the data, not the metric.
- Keep a held-out set untouched. Use training data to fit, a validation set to tune, and a test set only for the final score. If you tune against the test set repeatedly, its score becomes optimistic.
- Split the way production will see data. For time-dependent problems, split by date. A random split lets the model see the future, and the score will not hold up.
- Prevent leakage. Leakage means information in the training data that will not exist at prediction time. Fit scalers and encoders on training data only. Keep rows from the same customer or session in the same split.
- Freeze the evaluation set by version. If the test set changes between runs, scores are not comparable. Version it like any other dataset.
Metrics need to match the problem
Accuracy misleads on imbalanced data. If 99% of transactions are legitimate, a model that predicts "legitimate" every time scores 0.99 and catches no fraud. Pick metrics from the cost of each error:
- Precision matters when false alarms are expensive.
- Recall matters when missed cases are expensive.
- F1 or PR-AUC summarize both on imbalanced data.
- Regression problems often use MAE or RMSE, chosen by whether large errors should be penalized more.
Your criteria should use the metric you set in the problem-framing stage. If the business metric and the offline metric disagree, record both and treat the offline metric as a proxy.
Slice evaluation
An overall score hides weak groups. A slice is a subset defined by a feature value or class, such as one region or one device type. The example below computes per-class accuracy on the iris test set.
import numpy as np
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
SEED = 42
X, y = load_iris(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=SEED, stratify=y
)
model = LogisticRegression(max_iter=200).fit(X_train, y_train)
pred = model.predict(X_test)
overall = accuracy_score(y_test, pred)
per_class = {
int(c): accuracy_score(y_test[y_test == c], pred[y_test == c])
for c in np.unique(y_test)
}
print("overall:", round(overall, 3))
for cls, acc in per_class.items():
print("class", cls, "accuracy:", round(acc, 3))Use stratify=y so each class appears in the test set in proportion. Small slices give noisy scores. A slice with 10 examples can swing by 10 points from one wrong prediction, so set a minimum slice size before enforcing a threshold on it.
Encode the criteria as a function
The gate below checks the floor and the comparison with production. It returns a plain boolean, so a pipeline step can call it and fail the build.
def should_promote(candidate_accuracy, production_accuracy, min_gain=0.0, floor=0.90):
"""Release gate: the candidate must clear an absolute floor and not be worse than production."""
if candidate_accuracy < floor:
return False
return candidate_accuracy >= production_accuracy + min_gain
print(should_promote(0.94, 0.92)) # True
print(should_promote(0.89, 0.85)) # False: below the absolute floor
print(should_promote(0.93, 0.95)) # False: worse than productionThe second call shows why both checks are needed. The candidate beats production (0.89 against 0.85), but it still fails the floor. Production may itself be a weak model, and "better than bad" is not a release criterion.
Setting `min_gain`
With min_gain=0.0, any tie or tiny improvement promotes the candidate. Test-set scores carry sampling noise, so a gain of 0.001 is probably not real. Set min_gain above the noise you see between runs with different seeds. Record each run's seed and metrics with your experiment tracker so you can measure that spread.
Common errors
- Comparing scores from different test sets. Candidate and production must be scored on the same frozen data.
- Tuning the gate after seeing results. If you lower the floor so a favorite model passes, the gate no longer protects you. Change criteria through review, not on release day.
- Trusting one run. A single seed can be lucky. For borderline cases, repeat training with several seeds or use cross-validation and compare the spread.
- Ignoring calibration. If downstream code uses predicted probabilities as probabilities, check calibration as well as ranking quality.
- Checking only offline metrics. Offline scores do not prove business value. Staged rollout and online monitoring, covered in later chapters, check real behavior.
Record the outcome
Log the gate inputs and result with the run, so each registry decision can be explained. This builds on the tracking in the previous chapter: store the metrics, the evaluation-set version, and the thresholds used. A reviewer can then answer "why did this version ship?" from the registry history alone.
Exercise
A candidate model scores 0.91 accuracy and production scores 0.90 on the same frozen test set. The floor is 0.90 and min_gain is 0.02. Does should_promote return True? What would you change if run-to-run seed variation is about 0.01?
Show answer
It returns False. The candidate clears the floor (0.91 >= 0.90), but 0.91 is less than 0.90 + 0.02 = 0.92. With seed variation of about 0.01, a gain of 0.01 is within noise, so a mingain of 0.02 is reasonable. If you want to release small real improvements, reduce the noise first, for example by averaging over several seeds or using cross-validation, instead of setting mingain to zero.
Exercise
Your fraud model has 99% accuracy on a test set where 99% of transactions are legitimate. Name two checks you would add to the release gate.
Show answer
Add a minimum recall (or precision) for the fraud class, and a slice check on important segments such as region or payment type, with a minimum slice size. PR-AUC or F1 on the fraud class are also valid. Accuracy alone cannot detect a model that never predicts fraud.