Sub topic 2
Picking a metric that reflects the goal
This chapter assumes you know supervised learning basics, common metrics such as accuracy, precision, recall and mean absolute error (MAE), and basic scikit-learn. A baseline score only means something relative to a metric, so you must choose the metric before you build or score any baseline.
Why the metric comes first
A metric is a function that turns predictions into one number. A baseline is a simple predictor whose score a better model must beat. If you change the metric after seeing results, you can make almost any model look good. Fix the metric, and write down why, before the first run.
A good metric has three properties:
- It reflects the real cost of errors in the product.
- It suits the data, for example the class balance.
- It is reported on a held-out split, never on training data.
Accuracy fails on imbalanced data
Suppose 5% of examples are positive, such as fraud cases. A model that always predicts "not fraud" is right 95% of the time and finds no fraud. The code below shows this with a majority-class baseline.
from sklearn.datasets import make_classification
from sklearn.dummy import DummyClassifier
from sklearn.metrics import accuracy_score, balanced_accuracy_score
from sklearn.model_selection import train_test_split
X, y = make_classification(n_samples=2000, weights=[0.95, 0.05], random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, stratify=y, random_state=0
)
baseline = DummyClassifier(strategy="most_frequent").fit(X_train, y_train)
pred = baseline.predict(X_test)
print("accuracy:", accuracy_score(y_test, pred))
print("balanced accuracy:", balanced_accuracy_score(y_test, pred))Accuracy comes out near 0.95. Balanced accuracy, the average of recall over each class, is 0.5, which is the score of a coin flip. The second number tells the truth about this baseline.
Choosing by task
| Task | Common metrics | Watch out for |
|---|---|---|
| Balanced classification | Accuracy, F1 | Few problems here |
| Imbalanced classification | Balanced accuracy, recall, precision, PR AUC | Plain accuracy hides failure on the rare class |
| Regression | MAE, RMSE | RMSE punishes large errors more than MAE does |
| Forecasting | MAE, scaled errors | Compare against a naive forecast |
MAE is in the units of the target, so a stakeholder can read it directly. RMSE squares errors first, so choose it when one large miss costs much more than several small ones.
Some metrics depend on a decision threshold, such as precision and recall. Others, such as ROC AUC and PR AUC, score the ranking of predictions across all thresholds. Use a threshold-free metric to compare models, then pick the threshold from the cost of errors.
Let the cost of errors decide
Ask which mistake is worse. A missed cancer screening (false negative) costs more than an extra follow-up test (false positive), so recall matters most. A spam filter that hides real email makes false positives costly, so precision matters more. When both matter, F1 combines them, but it assumes they matter equally. If they do not, say so and weight them.
Common errors
- Reporting accuracy on imbalanced data without a majority-class baseline next to it.
- Choosing a metric because it is the library default.
- Using one metric for model comparison and a different one for the business decision, without stating how they relate.
- Averaging a metric over the whole dataset when the cost differs between groups. Later chapters cover slice evaluation.
Exercise
A model for a rare disease (1% positive) scores 99% accuracy on a test set. A majority-class baseline scores the same. Which metric would you switch to, and what result would show the model is useful?
Show answer
Switch to a metric that looks at the rare class, such as recall with precision, PR AUC, or balanced accuracy. Because missed cases are costly, recall at an acceptable precision is the most direct choice. The model is useful only if it scores clearly above the baseline on that metric, for example balanced accuracy well above 0.5, on the same held-out split.