Sub topic 3
Choosing a metric that matches the goal
This chapter assumes you can fit scikit-learn models, split data into train and test sets, and read a confusion matrix. A metric is the number you use to decide which model wins. If it doesn't match what the business cares about, you will pick the wrong model, however carefully you validate.
Start from the cost of mistakes
Ask what a wrong prediction costs. A missed fraud case costs far more than a false alarm. An overestimated house price costs about the same as an underestimated one. Your answers narrow the choice of metric.
Common metrics
| Metric | Task | Use it when | Watch out for |
|---|---|---|---|
| Accuracy | Classification | Classes are balanced and errors cost the same | Misleading on rare classes |
| Precision | Classification | False alarms are expensive | Ignores missed positives |
| Recall | Classification | Missed positives are expensive | Ignores false alarms |
| F1 | Classification | You need one number balancing precision and recall | Depends on the threshold |
| ROC AUC | Classification | You care about ranking positives above negatives | Looks optimistic on rare classes |
| Average precision | Classification | The positive class is rare | Baseline equals prevalence |
| MAE | Regression | Errors matter in proportion to their size | Hides rare large errors |
| RMSE | Regression | Large errors are disproportionately bad | Sensitive to outliers |
Accuracy fails on imbalanced data
The example below uses data where about 95% of rows are negative. A model that always predicts "negative" scores about 95% accuracy and has learned nothing.
from sklearn.datasets import make_classification
from sklearn.dummy import DummyClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, average_precision_score, f1_score, roc_auc_score
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=2000, weights=[0.95], flip_y=0.01, random_state=0)
X_tr, X_te, y_tr, y_te = train_test_split(X, y, stratify=y, random_state=0)
models = {
"always majority": DummyClassifier(strategy="most_frequent"),
"logreg": LogisticRegression(max_iter=1000),
}
for name, model in models.items():
model.fit(X_tr, y_tr)
pred = model.predict(X_te)
proba = model.predict_proba(X_te)[:, 1]
print(
name,
"acc", round(accuracy_score(y_te, pred), 3),
"f1", round(f1_score(y_te, pred, zero_division=0), 3),
"auc", round(roc_auc_score(y_te, proba), 3),
"ap", round(average_precision_score(y_te, proba), 3),
)The dummy model gets high accuracy, but its F1 is 0 and its AUC is 0.5. Average precision shows the same gap. Its baseline equals the share of positives, so you can see how much a model adds.
Threshold-free and threshold-based metrics
ROC AUC and average precision score the predicted probabilities, so they judge how well a model ranks rows. Precision, recall and F1 score hard labels, so they depend on the cutoff. By default the cutoff is 0.5, and that is rarely the best choice for imbalanced data. A good workflow compares models with a threshold-free metric. Then it tunes the threshold on validation data for the final decision.
Match the metric to the use
- Ranking leads for review: use average precision, or precision at the top k.
- Probabilities feed a downstream calculation: check calibration, for example with the Brier score.
- Regression with a skewed target: consider MAE, or take the log of the target first.
Pick the metric before you compare models, and write down why. Use the same metric for cross-validation, tuning and the final report. Changing it after you see results lets your preferences leak into the evaluation.
Exercise
A model flags fraudulent transactions, and about 0.5% are fraud. Investigators can only review a few hundred flagged cases per day. Why is accuracy a poor metric here, and which metric would you choose?
Show answer
A model that never flags fraud would score about 99.5% accuracy, so accuracy says nothing about quality. Investigators review a limited list ranked by score, so average precision or precision at the top k matches the goal. Both reward putting real fraud near the top of the ranking.