How do you handle imbalanced datasets in machine learning classification?
Imbalanced datasets where one class significantly outnumbers others (e.g., 99% negative, 1% positive in fraud detection) cause classifiers to achieve high accuracy by simply predicting the majority class. Techniques: (1) Data-level: oversampling minority class (SMOTE generates synthetic examples by interpolating between minority class neighbors), undersampling majority class (random or informed selection like Tomek links), and combination methods (SMOTE + Tomek links for cleaning). (2) Algorithm-level: class weights (assign higher loss weight to minority class, most frameworks support this via classweight parameter), focal loss (down-weight easy examples, focus on hard ones, effective for extreme imbalance), and cost-sensitive learning (different misclassification costs per class). (3) Ensemble: balanced bagging (sample balanced subsets for each ensemble member), EasyEnsemble (undersample majority class into multiple subsets, train a model on each), and balanced random forest. (4) Threshold tuning: train on imbalanced data but optimize the classification threshold on the validation set using precision-recall curves rather than defaulting to 0.5. Evaluation: never use accuracy for imbalanced datasets. Instead use precision, recall, F1-score (especially for the minority class), area under the precision-recall curve (AUPRC, more informative than AUROC for imbalanced data), and Matthews Correlation Coefficient (MCC, balanced measure). Best practices: understand the domain costs (in fraud detection, missing fraud is much costlier than false alarms), start with class weights (simplest to implement), use stratified cross-validation to maintain class proportions in each fold, and avoid generating too many synthetic samples (can cause overfitting to synthetic patterns).