Sub topic 12
Common pitfalls when reporting metrics
This chapter assumes you can compute a confusion matrix, accuracy, precision, recall, and F1, and that you know what a train/test split is. It covers mistakes that make a correct formula produce a misleading number.
Pitfall 1: scoring on data the model has seen
A model can memorize its training data. Scoring it on that data tells you how well it remembers, not how well it predicts new cases.
from sklearn.datasets import make_classification
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier
X, y = make_classification(n_samples=300, n_features=10, flip_y=0.1, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.3, random_state=0
)
model = KNeighborsClassifier(n_neighbors=1).fit(X_train, y_train)
print(accuracy_score(y_train, model.predict(X_train))) # 1.0
print(accuracy_score(y_test, model.predict(X_test))) # lower than 1.0A 1-nearest-neighbor model finds each training point as its own nearest neighbor, so its training accuracy is 1.0. The test score is the honest one. The labels here are deliberately noisy (flip_y=0.1), so a perfect score on the unseen data is impossible.
The safe order is below.
Pitfall 2: undefined ratios
Precision is tp / (tp + fp). If the model never predicts the positive class, tp + fp is 0 and the ratio is undefined. Recall has the same problem when the data contains no true positives.
from sklearn.metrics import precision_score, recall_score
y_true = [1, 1, 0, 0]
y_pred = [0, 0, 0, 0] # the model never predicts positive
print(precision_score(y_true, y_pred)) # 0.0, with a warning
print(precision_score(y_true, y_pred, zero_division=0)) # 0.0, no warning
print(precision_score(y_true, y_pred, zero_division=1)) # 1.0
print(recall_score(y_true, y_pred)) # 0.0By default, scikit-learn returns 0.0 and raises an UndefinedMetricWarning. The zero_division argument makes the choice explicit. Do not ignore the warning. It often means the model predicts only one class, or a class is missing from a small evaluation slice. Setting zero_division=1 can make a useless model look perfect, so use it only when you can justify it.
Pitfall 3: a single number with no context
"The model has 95% accuracy" says little. Before trusting any reported metric, check these points:
- Class balance. Accuracy of 0.99 on data with 1% positives matches a model that never predicts positive.
- Which class is positive. Precision and recall depend on which label you call positive. Swapping the labels gives different numbers.
- The threshold. Precision and recall change when the threshold moves. A report should state the threshold, usually 0.5 by default.
- The averaging method. In multiclass problems, macro, micro, and weighted averages can differ a lot. Name the one you used.
- The sample. State how many examples were evaluated. Precision of 1.0 from 3 predictions is weak evidence.
Pitfall 4: comparing metrics across different datasets
A recall of 0.75 on one test set and 0.80 on another do not show that the second model is better if the sets differ in difficulty or class mix. Compare models on the same fixed test set. With small sets, differences of a few points can be noise.
A reporting checklist
| Check | Question to ask |
|---|---|
| Data | Were the metrics computed on unseen data? |
| Balance | What fraction of examples is positive? |
| Positive class | Which label counts as positive? |
| Threshold | What cutoff turned scores into labels? |
| Averaging | Binary, macro, micro, or weighted? |
| Zero division | Did any warning appear, and how was it handled? |
| Sample size | How many examples were evaluated? |
Exercise
A fraud model is evaluated on 200 test transactions that contain 4 frauds. It predicts "not fraud" for every transaction. A colleague reports "98% accuracy" and calls it excellent. Compute accuracy, recall, and precision with scikit-learn's default handling, and explain what the report should have included.
Show answer
Accuracy is 196 / 200 = 0.98. Recall is 0 / 4 = 0.0, because no fraud was found. Precision is 0 / 0, which is undefined. scikit-learn returns 0.0 and warns unless zero_division is set. The report should have stated the class balance (2% positive), included the confusion matrix (TP=0, FP=0, TN=196, FN=4), and given recall, since missing every fraud makes the model useless despite the 98% accuracy.