Handling Class Imbalance — ML Technical Interview Questions & Answers

Oversampling, undersampling and SMOTE.

Handling Class Imbalance

  • Simple meaning: One class is rare (5 fails, 95 pass). Accuracy is a trap. The model can ignore fails and still look “good”.
  • Fixes: class_weight='balanced', resample (undersample pass, oversample fail), SMOTE (synthetic fail rows — use with care), and a metric that sees the rare class (F1, recall, PR AUC).
  • Picture: Always predict pass → 95% accuracy, 0 recall on fail. That model must not ship.
  • Trap: SMOTE before the train/test split (leak). Trap: oversampling until the sheet is fiction and test is still 5% fails — report a real-world metric.

Technical Interview Tip: Rare fails: don’t use accuracy. Weight or resample after the split.

Code Examples for Handling Class Imbalance

Handling Class Imbalance — working example

colors = ["red", "blue", "green"]
value = "blue"
vec = [1 if c == value else 0 for c in colors]
print(vec)

# Class imbalance idea: accuracy can look high while minority recall is poor
y_true = [0, 0, 0, 0, 1]
y_pred = [0, 0, 0, 0, 0]
acc = sum(t == p for t, p in zip(y_true, y_pred)) / len(y_true)
print("accuracy", acc, "— missed the only positive")

Handling Class Imbalance — one-hot encodes categories as sparse binary vectors; imbalance needs recall/precision, not accuracy alone.

How to Explain Handling Class Imbalance to an Interviewer

Why is imbalance a problem?

Model ignores the rare class Accuracy still looks high

Name two fixes.

class_weight resample F1/recall