Machine Learning · medium

How do you handle class imbalance in classification?

Asked in ML Engineer interviews, in the Machine Learning round.

Short answer

Resampling (SMOTE/undersample), class weights, threshold tuning, PR-AUC over accuracy.

How to answer it

First ask whether it is a problem. A 1% positive rate is fine if the model ranks well; it is only a problem for a metric like accuracy, or for a model that never predicts the minority. So the first fix is measurement: precision, recall, PR-AUC, not accuracy.

Then, in order of preference:

from sklearn.linear_model import LogisticRegression
clf = LogisticRegression(class_weight="balanced").fit(X_tr, y_tr)
p = clf.predict_proba(X_val)[:, 1]
# choose the threshold, not 0.5
from sklearn.metrics import precision_recall_curve
prec, rec, thr = precision_recall_curve(y_val, p)

Related questions

Practice this for real