Class Imbalance

Class imbalance means the class prior is highly uneven. The problem is not rarity by itself; it is that default training objectives, thresholds, and evaluation metrics may optimize the majority class while missing the decision that matters.

Why accuracy misleads

Imbalance makes accuracy a trap. Suppose transactions contain only fraud cases (the positive class) and legitimate ones. A model that always predicts “not fraud” produces this confusion matrix:

actual fraud (10)actual legit (990)
predicted fraud
predicted legit

Its accuracy is , yet its recall on the class that matters is — it catches no fraud at all. Under imbalance, classification is really a ranking and decision-cost problem: how many cases can be reviewed, and what does missing a positive cost?

Metrics under imbalance

Let be the prevalence of the positive class. A binary rule thresholds a model score at a cutoff , predicting positive when , where the indicator is when its condition holds. Writing , , and for the true-positive, false-positive, and false-negative counts, minority-class precision and recall are

Balanced accuracy averages recall across classes; in binary classification it is . Here sensitivity, or true positive rate, is ; specificity, or true negative rate, is . Sensitivity asks how many actual positives were caught. Specificity asks how many actual negatives were correctly rejected.

PR-AUC summarizes the precision-recall curve for the rare positive class. A common discrete summary is average precision (AP):

where and are precision and recall after moving the threshold through the ranked predictions. AP is a weighted average of precision values: a precision value gets weight only when recall increases. That happens when the next item admitted by the moving threshold is an actual positive.

The ranked-list view is the easiest way to read AP:

  1. Sort examples by predicted positive-class score from highest to lowest.
  2. Start with a threshold above the highest score, so no example is predicted positive.
  3. Move down the ranking one example at a time.
  4. After each newly admitted example, recompute precision and recall.
  5. Add area only when recall increases; false positives lower later precision but do not directly increase recall.

Suppose the positive class is rare fraud and the model ranks six transactions like this:

RankLabelPrecision after this rankRecall after this rankAP contribution
11
20
31
40
50
61

There are three positives, so recall increases by each time a positive appears. The average precision is:

This score rewards putting positives early. The false positives at ranks 2, 4, and 5 do not add AP area directly, but they reduce precision at the later positive ranks. That is why AP is often more diagnostic than ROC-AUC when the negative class dominates: it focuses on how clean the high-score review queue is for the rare class.

Class weighting changes empirical risk to , where is larger for rare or costly classes.

Comparing plain and weighted models

The snippet compares the same imbalanced dataset with and without class weighting. The point is not that weighting is always better; it is that it changes the precision-recall tradeoff.

from sklearn.datasets import make_classification
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
from sklearn.model_selection import train_test_split
 
X, y = make_classification(n_samples=500, n_features=6, n_informative=3,
                           weights=[0.95, 0.05], random_state=11)
Xtr, Xte, ytr, yte = train_test_split(X, y, stratify=y, random_state=11)
for name, est in [("plain", LogisticRegression(max_iter=1000, random_state=11)),
                  ("balanced", LogisticRegression(max_iter=1000, class_weight="balanced", random_state=11))]:
    est.fit(Xtr, ytr)
    pred = est.predict(Xte)
    p, r, _, _ = precision_recall_fscore_support(yte, pred, zero_division=0)
    print(name, "accuracy", round(accuracy_score(yte, pred), 3),
          "minority_precision", round(p[1], 3), "minority_recall", round(r[1], 3))

Observed output:

plain accuracy 0.952 minority_precision 1.0 minority_recall 0.143
balanced accuracy 0.776 minority_precision 0.08 minority_recall 0.286

The balanced model catches more minority examples but creates many more false positives. Whether that is better depends on intervention cost.

Caveats

Resampling before splitting leaks duplicates or synthetic information across folds. PR-AUC is usually more informative than ROC-AUC under extreme rarity, but it still does not choose an operating threshold. Reweighting can hurt calibration, so check probability reliability separately.

References