Sub topic 3
Framing the Problem and Choosing Success Metrics
Assume you can train and evaluate a supervised model in Python, use basic Git and command-line tools, and understand how a web service is deployed. This chapter focuses on the decision that comes before model training: defining what the model should predict and how the organization will decide whether it is useful.
Start with an operational decision
A machine learning problem is not simply “predict a label.” It is a decision process with four parts:
- Action: What will someone or some system do?
- Prediction: What must the model estimate to support that action?
- Outcome: What eventually happens in the real world?
- Constraint: What limits cost, delay, risk, or fairness?
For example, an online retailer might ask, “Which orders are likely to arrive late?” The operational decision could be to notify customers or select a faster shipping method. The prediction is late delivery risk. The outcome is whether delivery actually misses its promised date.
This distinction prevents a common error: optimizing a prediction that nobody uses. A model with excellent accuracy has little value if its predictions arrive after the decision window or do not change an action.
Define the target and measurement window
Write the target as a precise rule. Include the population, prediction time, look-ahead period, and label definition.
For late delivery, a precise definition might be:
- Population: orders accepted before 18:00.
- Prediction time: when the order is packed.
- Look-ahead period: the promised delivery date.
- Positive label: delivered after the promised date.
This specification also exposes data leakage. Data leakage is information used during training that would not be available when the prediction is made. A delivery scan recorded after the promised date may help predict lateness during analysis, but it cannot be an input at packing time.
Choose metrics that match the cost of errors
Accuracy is the fraction of predictions that are correct. It can be misleading when classes are imbalanced. If only 2% of orders are late, predicting “on time” for every order gives 98% accuracy while finding no late orders.
For a binary classifier, a confusion matrix counts:
- True positives: late orders correctly flagged.
- False positives: on-time orders incorrectly flagged.
- False negatives: late orders missed.
- True negatives: on-time orders correctly ignored.
Precision measures how many flagged orders are truly late. Recall measures how many late orders the model catches. Select a metric based on the action's costs.
| Situation | Useful primary metric | Reason |
|---|---|---|
| Missing a risky case is expensive | Recall | Find more positive cases |
| Investigations are limited | Precision | Make alerts more trustworthy |
| Both error types matter | F1 score or cost | Balance or price the errors |
A threshold is a policy choice, not just a model property. Lowering a risk threshold usually increases recall and decreases precision. Set the threshold using validation data and an explicit cost model.
def expected_cost(y_true, y_pred, false_positive_cost, false_negative_cost):
false_positives = sum(
actual == 0 and predicted == 1
for actual, predicted in zip(y_true, y_pred)
)
false_negatives = sum(
actual == 1 and predicted == 0
for actual, predicted in zip(y_true, y_pred)
)
return (
false_positives * false_positive_cost
+ false_negatives * false_negative_cost
)Define success before training
Use at least one model metric and one operational metric. For example:
- Recall must be at least 0.80 on a recent holdout set.
- False-positive cost must remain below a defined budget.
- Predictions must be available within 200 milliseconds.
- The intervention must reduce late deliveries without increasing cancellation rates.
These criteria turn model evaluation into a release decision. They also make later monitoring meaningful because production measurements can be compared with the original agreement.
Exercise
A fraud model has 99% accuracy, but fraud occurs in 1% of transactions and the model flags none. What should you investigate first?
Show answer
Investigate class imbalance and choose metrics such as recall, precision, and expected error cost that reflect the fraud decision.