M2-071M2: Classical Machine LearningClass Imbalance LearningEasy
⚡Flashcard Drill➔Mastery:

Major Approaches for Handling Class Imbalance

📐 Mathematical Definition
class weight: wc=N2Nc\text{class weight}:\ w_c=\frac{N}{2N_c}
⚡ One-Sentence Core Answer
Address imbalance at the data level (re-sampling), algorithmic level (cost-sensitive loss, Focal Loss), and post-processing level (threshold tuning).

📌 Key Takeaways

  • •
    Data-level: random undersampling, oversampling, SMOTE, ADASYN
  • •
    Algorithm-level: class-weighted loss, Focal Loss, balance cascade ensembling
  • •
    Post-processing: probability calibration followed by cost-optimal threshold shifting

📐 Mathematical Derivations

Taxonomy of Class Imbalance Solutions: ① Re-sampling: Adjust the empirical class distribution P(y)P(y) to P′(y)P'(y). Undersampling drops majority samples (discards data); Oversampling replicates minority samples (risks overfitting); Synthetic generation (SMOTE) interpolates between nearest minority neighbors. ② Cost-Sensitive Learning: Modify the loss objective L=−∑iwyiℓ(yi,y^i)\mathcal{L} = - \sum_i w_{y_i} \ell(y_i, \hat{y}_i) with inverse-frequency weights wc=NK⋅Ncw_c = \frac{N}{K \cdot N_c}. ③ Focal Loss: LFL=−αt(1−pt)γlog⁡pt\mathcal{L}_{\text{FL}} = - \alpha_t (1 - p_t)^\gamma \log p_t, dynamically downweighting easy negative examples. ④ Threshold Moving: Adjust decision boundary τ\tau based on Bayes risk or business trade-offs.

🏭 Production Trade-offs

Engineering decision matrix: In massive datasets (N>108N > 10^8), Random Undersampling combined with ensembling (e.g., EasyEnsemble) is computationally optimal. In medium datasets, Class Weights or Focal Loss are preferred because they preserve original data distribution and maintain consistent sample sizes. For business KPIs, always couple model training with rigorous post-calibration threshold optimization.
⚠️ Common Interview Pitfalls
  • ✕
    Evaluating models on imbalanced data using accuracy instead of PR-AUC, F1, or cost-weighted matrices
  • ✕
    Applying SMOTE to the entire dataset before train/test splitting, leaking synthetic test points into training
🎯 Interviewer Follow-up Inquiries
  • ?
    When is random undersampling strictly superior to SMOTE in large-scale production systems?
  • ?
    Why does modifying class weights during training distort predicted probabilities, and how do you calibrate them?
⚡
Test muscle memory on this topic via 3-Layer Flashcards
Active Recall: Mentally formulate Prompt ➔ Math Derivation ➔ Benchmark Implementation in 3 minutes.
🎯 Drill This Card Now➔
📚

Associated Knowledge Base Guides & Mindmaps

Explore the comprehensive technical article, exam cards, and global architecture tree.

← PreviousM2-070: How to Evaluate Feature Selection Effectiveness? Key Considerations and Validation📋Back to BankNext →M2-072: SMOTE Algorithm: Core Mechanism, Theoretical Risks, and Limitations
中文速览与面试要点

处理类别不平衡的主要方法有哪些?

重采样(过/欠采样)、代价敏感、阈值调整、集成、异常检测视角。

核心要点

  • 五个层次的解法(从数据到决策):① 指标层面 ——首先改用对不平衡敏感的指标(PR-AUC、F1、召回@固定精度),而非 accuracy 或 ROC-AUC(它们在不平衡下过于乐观);这是最容易被忽视但最重要的一步。② 数据层面 —— 过采样 (复制少数类、SMOTE 合成)或 欠采样 (丢弃多数类);过采样不丢信息但增加训练时间与过拟合风险(复制样本),欠采样快但丢信息;两者都必须在 CV 折内 做(否则合成/复制的样本会跨越折边界导致泄漏)。③ 算法层面 —— 代价敏感 (给少数类更高的损失权重,如 class_weight='balanced')、 Focal Loss (降低易分类样本的权重)、或专门的不平衡学习方法。④ 决策层面 —— 调整阈值 :训练后用验证集选使业务目标(如代价矩阵最小化)最优的阈值,而非默认 0.5。⑤ 问题重构 ——把少数类检测视为 异常检测 (单类分类、孤立森林),或用 集成 (BalancedBagging、EasyEnsemble)。
  • 实践要点:① 过采样 vs 欠采样 ——多数场景下 代价敏感/类权重 优于重采样(不改变数据分布、更稳定);若必须重采样,过采样(SMOTE)通常优于欠采样(不丢信息),但 SMOTE 在高维/类别重叠区会生成错误样本。② SMOTE 的适用与风险 ——在少数类近邻间线性插值;适合 数值特征且类别可分 的场景;风险是 (a) 在类别边界生成'侵入'多数类的样本,(b) 高维下近邻不可靠,(c) 对类别特征不适用(需 SMOTE-NC)。③ 不要对验证/测试集重采样 ——只在训练折上做,验证/测试集保持原始分布(否则评估失真)。④ 类权重的等价性 ——类权重 ≈ 按权重重采样,但更稳定(不改变样本数、无重复);这是首选方案。⑤ 阈值优化的前提 ——模型需输出 校准良好的概率 (否则阈值无意义);若模型过自信(如深度网络),应先做温度缩放等校准。⑥ 业务视角 ——最终应以业务代价决定权衡(如欺诈检测漏判代价 >> 误判代价,则优先召回)。
  • 先问'指标是否合适'(用 PR-AUC 而非 ROC-AUC)
  • 重采样需在 CV 折内做

常见陷阱

  • 对验证/测试集做重采样(评估失真)
  • 在不平衡数据上用 accuracy 或 ROC-AUC 作为主指标