Class imbalance

core 10 minmodel-evaluationimbalance

Builds on:Classification metrics

The idea

With a 1% positive rate, the model can be 99% accurate by doing nothing. Imbalance breaks the silent assumptions of default classification — that 0.5 is a sensible threshold, that accuracy measures usefulness, and that both error types cost the same. The fixes live at three layers:

1. The metric layer. Accuracy → precision/recall and PR curves. The mechanical reason ROC flatters: FPR =FP/(FP+TN)= FP/(FP+TN) has the giant negative class in its denominator, so an avalanche of false alarms barely dents it, while precision =TP/(TP+FP)= TP/(TP+FP) takes the hit in full. Judge PR-AUC against its no-skill baseline — the prevalence, not 0.5. Use stratified CV so folds contain positives at all.

2. The training layer. Make rare examples matter:

3. The decision layer — usually the right one. Imbalance is really a cost asymmetry problem. With calibrated probability pp, a missed-positive cost CFNC_{FN}, and a false-alarm cost CFPC_{FP} that applies only when the case is actually negative, flag when expected cost says so:

p⋅CFN>(1−p)⋅CFP⟺p>CFPCFP+CFNp \cdot C_{FN} > (1-p) \cdot C_{FP} \quad\Longleftrightarrow\quad p > \frac{C_{FP}}{C_{FP} + C_{FN}}

If taking the action has a fixed cost CactionC_{action} for every flagged case (such as a manual review), compare CactionC_{action} directly with pCFNpC_{FN}: flag when p>Caction/CFNp > C_{action}/C_{FN}.

The 0.5 default corresponds to equal costs — almost never true when one class is rare. A well-ranked, well-calibrated model plus the right threshold often beats any resampling scheme, and it keeps probabilities honest for downstream use. Caveat for rebalancers: training on altered prevalence distorts the posterior — recalibrate on true-prevalence data or apply the known prior correction.

Threshold lab

Move one decision threshold. The model scores stay fixed; every metric and cost changes around that choice.

TP
70
FP
270
FN
30
TN
630
Precision
20.6%
Recall
70%
F1
31.8%
Accuracy
70%
Cost / 1k
$17,700

Cost assumes $10 per false alarm and $500 per miss. Lower thresholds catch more positives; prevalence determines how many false alarms surround them.

Worked example

Fraud detection: 10,00010{,}000 transactions, prevalence 1% → 100 fraud, 9,900 legitimate. Model operates at TPR =0.80= 0.80, FPR =0.05= 0.05:

TP=80,FN=20,FP=0.05×9900=495,TN=9405TP = 80, \quad FN = 20, \quad FP = 0.05 \times 9900 = 495, \quad TN = 9405

Precision=8080+495=80575≈0.139\text{Precision} = \frac{80}{80 + 495} = \frac{80}{575} \approx 0.139

On the ROC plot, (FPR 0.05, TPR 0.80) looks like a strong classifier. In the queue a human reviews, 6 of every 7 alerts are false — the ROC point and the analyst experience are describing the same model.

Now the cost view. A missed fraud costs $500; every manual review costs $10:

threshold=CreviewCFN=10500=0.02\text{threshold} = \frac{C_{review}}{C_{FN}} = \frac{10}{500} = 0.02

Flag anything with fraud probability above 2% — twenty-five times more aggressive than the 0.5 default. Sanity-check with expected cost at this operating point: the current confusion matrix costs 20×500+575×10=15,75020 \times 500 + 575 \times 10 = 15{,}750 dollars per 10k transactions. A blanket-review policy would cost 10,000×10=100,00010{,}000 \times 10 = 100{,}000 dollars; doing nothing costs 100×500=50,000100 \times 500 = 50{,}000 dollars. The model earns its keep only through where you put the threshold — and 0.5 was never a candidate.

Cheat sheet

Cost-optimal threshold (calibrated pp): with a false-alarm-only cost, flag when p>CFPCFP+CFNp > \frac{C_{FP}}{C_{FP}+C_{FN}}; with a fixed action/review cost on every alert, flag when p>Caction/CFNp > C_{action}/C_{FN}. Equal false-positive/false-negative costs → 0.5; rare-and-costly misses → tiny threshold.

PR vs ROC: rare positives + flagging workload ⇒ PR (baseline = prevalence). Balanced classes / prevalence-free comparison ⇒ ROC (baseline 0.5).

ToolWhat it doesWatch out
Threshold tuningmoves operating point, model untouchedneeds calibrated or at least monotone scores
Class weightsreweights loss toward minoritydistorts calibration; recalibrate after
Random oversamplingduplicates minority rowsoverfits duplicates
Random undersamplingdrops majority rowsdiscards signal; try ensembles of undersamples
SMOTEinterpolates synthetic minority pointsleaks if before split; fabricates points in overlap regions
Cost-sensitive learningbakes CFP,CFNC_{FP}, C_{FN} into trainingrequires actually knowing the costs