99% accuracy on fraud data. Is the model any good?
Question
A payments team tests two fraud models on 1,000 payments. 10 of them are fraud.
- Model A says "legit" for every payment. Accuracy: 99.0%.
- Model B flags 20 payments, and 6 of them are fraud. Accuracy: 98.2%.
The team wants to ship Model A because its accuracy is higher. Is that right?
Compute precision and recall for each model. Which one would you keep, and what would you measure instead of accuracy?
Show hints (3)
- "THE LAZY BASELINE"
What accuracy do you get by saying "legit" every time? If doing nothing scores 99%, what does 99% tell you?
- "COUNT FOUR OUTCOMES"
For Model B, count frauds caught, legit payments flagged, frauds missed and legit payments let through. The four counts must add up to 1,000.
- "LOOK ONLY AT FRAUD"
Precision: of the flagged payments, how many are fraud? Recall: of all frauds, how many were flagged? Model A flags nothing, so its recall is 0.
Show checked explanation
No. With 1% fraud, a model that never flags anything gets 99% accuracy and catches no fraud. Model B catches 6 of the 10 frauds: precision 0.30, recall 0.60. On a rare class, judge a model by precision, recall and the cost of each error, not by accuracy.
import numpy as np from sklearn.metrics import (accuracy_score, confusion_matrix, precision_score, recall_score) y_true = np.array([1] * 10 + [0] * 990) # 1 = fraud: 10 of 1,000 model_a = np.zeros(1000, dtype=int) # "legit" for every payment model_b = np.array([1] * 6 + [0] * 4 + [1] * 14 + [0] * 976) for name, y_pred in [("A", model_a), ("B", model_b)]: tn, fp, fn, tp = confusion_matrix(y_true, y_pred).ravel() acc = accuracy_score(y_true, y_pred) prec = precision_score(y_true, y_pred, zero_division=0) rec = recall_score(y_true, y_pred) print(f"{name}: TP={tp} FP={fp} FN={fn} TN={tn} " f"accuracy={acc:.3f} precision={prec:.2f} recall={rec:.2f}")
A: TP=0 FP=0 FN=10 TN=990 accuracy=0.990 precision=0.00 recall=0.00 B: TP=6 FP=14 FN=4 TN=976 accuracy=0.982 precision=0.30 recall=0.60
Code run on CPython 3.12 with scikit-learn 1.9 against the examples above. Checked by an editor other than the author.
Walkthrough
Fraud is the positive class. TP is a fraud the model flags, FP a legit payment it flags, FN a missed fraud and TN a legit payment let through.
| model | TP | FP | FN | TN | accuracy | precision | recall |
|---|---|---|---|---|---|---|---|
| A | 0 | 0 | 10 | 990 | 0.990 | 0 | 0 |
| B | 6 | 14 | 4 | 976 | 0.982 | 0.30 | 0.60 |
Accuracy is (TP + TN) / all. Model A gets 990 / 1,000 by saying "legit" every time, so its 99% only repeats the share of legit payments. Model B makes 18 mistakes instead of 10, but it catches 6 frauds, and A catches none.
Two metrics look only at fraud:
- Precision = TP / (TP + FP) = 6 / 20 = 0.30. Of B's flags, 30% are fraud.
- Recall = TP / (TP + FN) = 6 / 10 = 0.60. B finds 60% of all frauds.
Model A's precision is 0 / 0. scikit-learn returns 0.0 and warns, unless you set zero_division.
Say a missed fraud costs $500 and a false alarm costs $5 of review time. Model A costs 10 × $500 = $5,000. Model B costs 4 × $500 + 14 × $5 = $2,070. Accuracy treats both errors as equal, so it misses this.
Three fixes, cheapest first:
- Move the threshold. The default cut at 0.5 assumes both errors cost the same. A lower cut catches more fraud at lower precision.
- Class weights.
class_weight="balanced"counts each fraud 50 times and each legit payment about 0.505 times in training. - Resampling. Oversample fraud or undersample legit rows, in the training data only. Use it when the model has no weights option, or the data is too big.
Common mistakes
- Reading 99% accuracy as "the model is 99% right about fraud". Here it is right about 0 of the 10 frauds.
- Resampling the test set too. Precision and recall then describe a fraud rate you will never see in production.
- Picking the model by F1 without asking about cost. F1 weighs precision and recall equally. Model B's F1 is 0.40, but a $500 miss and a $5 false alarm are not equal.
- Reading scores from a class-weighted or resampled model as fraud chances. Both train the model as if fraud were common, so its scores run well above the real 1% rate. Recalibrate them first.
Follow-up questions
- "How do you choose the threshold?" On a validation set, not the test set.
precision_recall_curvegives precision and recall at every threshold. Pick the lowest total cost, or the highest recall at a precision the review team can handle. - "Class weights or resampling: which do you try first?" Usually class weights. They change the loss, not the data, so every row is still used. Undersampling throws rows away but trains faster on huge data. Oversample only after the split, or copies of one fraud land in both train and validation.
What a strong answer covers
Spots that never flagging fraud already scores 99%. Builds the confusion matrix, computes precision and recall, and ties the choice to the cost of each error. Knows when to move the threshold, reweight or resample.
Open in interactive practice deck →