EasyDeepLearn
Supervised Learning · section 14 of 18

Imbalanced data

5 interview questions on imbalanced data, each answered in full. Free to read, no account needed.

How do you handle class imbalance in a dataset?

medium
  • Options: (1) resampling — oversample minority (SMOTE) or undersample majority; (2) class weights in the loss function; (3) threshold tuning on the probability output; (4) anomaly-detection framing when positives are extremely rare; (5) collect more minority data.
  • Always evaluate with PR-AUC, F1 or recall at fixed precision — not plain accuracy.
#imbalance#data-qualityPermalink & quiz →

SMOTE, Borderline-SMOTE, SVM-SMOTE, ADASYN — when do you use each?

hard
  • Vanilla SMOTE oversamples uniformly, including points deep in the class interior (redundant).
  • Borderline-SMOTE: only synthesize from minority points near the decision boundary (their kNN mixes both classes) — where the model actually needs help.
  • SVM-SMOTE: fit an SVM, synthesize near the support vectors.
  • ADASYN: synthesize more around minority points that are hard to classify (many majority neighbours), less around easy ones — adaptive.
  • In practice: try Borderline-SMOTE and ADASYN before vanilla — usually a small but consistent improvement.

Random undersampling vs Tomek links vs cluster-centroid undersampling — what's each for?

medium
  • Random undersampling: drop majority-class rows uniformly to balance.
  • Fast; wastes majority-class information.
  • Tomek links: identify pairs of opposite-class points that are each other's nearest neighbours (boundary noise) and drop the majority-class member — cleans the decision boundary.
  • Cluster centroids: cluster majority points, replace them with cluster centroids — preserves diversity while reducing count.
  • Combine SMOTE + Tomek or SMOTE + ENN (edited nearest neighbours) to oversample the minority and clean the boundary in one pass.

What is focal loss and why does it help with class imbalance?

hard
  • Focal loss = -(1    pt)γ    log(pt)(1\; - \;p_{t})\gamma\; \cdot \;\operatorname{log}(p_{t}), a modification of cross-entropy that adds a modulating factor (1    pt)γ(1\; - \;p_{t})\gamma.
  • When ptp_{t} is high (easy example), the loss is heavily down-weighted; when ptp_{t} is low (hard example), the modulator is close to 1 — focus on hard examples. γ  =  0\gamma\; = \;0 recovers cross-entropy; γ  =  2\gamma\; = \;2 is the standard for RetinaNet.
  • Combined with alpha per class, it addresses both easy-example overwhelm and class imbalance in the same objective — dominant loss in dense object detection.

Class weights vs resampling for imbalance — which do you reach for first?

easy
  • Class weights (or a weighted loss) first — they're a one-line change, preserve the dataset, don't discard data or synthesize points, and often match resampling in accuracy.
  • If class weights aren't enough (extremely rare positives, non-linear boundary near the minority), try SMOTE variants for oversampling.
  • Combine with threshold tuning at inference for full control.
  • Real answer for tabular data with mild imbalance: classweight\mathrm{class}_{\mathrm{weight}}='balanced' + threshold tuning is usually enough.

Practise Supervised Learning