M2-075M2: Classical Machine LearningClass Imbalance LearningHard
⚡Flashcard Drill➔Mastery:

Cost-Sensitive Learning: Implementation and Relationship with Re-sampling

📐 Mathematical Definition
L=−∑iwyilog⁡pyi\mathcal L=-\sum_i w_{y_i}\log p_{y_i}
⚡ One-Sentence Core Answer
Incorporate class weights into the loss function; it is asymptotically equivalent to re-sampling but avoids data duplication and stabilizes empirical training variance.

📌 Key Takeaways

  • •
    Loss weighting: L=−∑iwyiℓ(yi,y^i)\mathcal{L} = - \sum_i w_{y_i} \ell(y_i, \hat{y}_i) with inverse class frequencies
  • •
    Equivalence with re-sampling: weighting an instance by rr contributes the same expected gradient as sampling it rr times
  • •
    Key divergence: re-sampling alters the empirical data distribution, affecting calibration and batch norm statistics

📐 Mathematical Derivations

Mathematical Connection: Expected risk under importance sampling: Ex∼P′[ℓ(x,y)]=Ex∼P[P′(x,y)P(x,y)ℓ(x,y)]\mathbb{E}_{x \sim P'}[\ell(x, y)] = \mathbb{E}_{x \sim P}\left[\frac{P'(x, y)}{P(x, y)} \ell(x, y)\right]. Oversampling the minority class by factor rr scales its empirical frequency from PP to P′P', which corresponds to multiplying its loss term by w=rw = r.
Divergence Points: ① Variance: Oversampling draws duplicate samples with replacement, introducing discrete covariance across mini-batch gradients; class weighting applies deterministic scaling without inflating batch sample count. ② Calibration: Re-sampling shifts the model's internal prior towards 50/50, requiring post-hoc log-odds correction: logit(p)=logit(presampled)−log⁡(r)\text{logit}(p) = \text{logit}(p_{\text{resampled}}) - \log(r). Class weighting maintains raw feature density.

🏭 Production Trade-offs

Implementation preference: In modern production pipelines, Class Weights or Focal Loss are preferred over oversampling due to zero memory overhead, reproducible deterministic batches, and absence of synthetic sample artifacts. Reserve undersampling for situations where the majority class is overwhelmingly large (>108>10^8 rows) and compute budget is the primary bottleneck.
⚠️ Common Interview Pitfalls
  • ✕
    Treating class-weighted model outputs as raw calibrated probabilities without performing Bayesian prior correction
  • ✕
    Believing oversampling provides new information, whereas it simply repeats existing samples and increases overfitting risk
🎯 Interviewer Follow-up Inquiries
  • ?
    How do you mathematically correct the output probabilities of a model trained on an undersampled dataset?
  • ?
    Under what conditions does Focal Loss provide superior gradients compared to simple inverse class weighting?
⚡
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-074: Classification Threshold Selection Strategies Across Diverse Business Objectives📋Back to BankNext →M2-076: Three Missing Data Mechanisms (MCAR, MAR, MNAR) and Treatment Strategies
中文速览与面试要点

代价敏感学习如何实现?与重采样的关系

在损失中给不同类别不同权重;等价于按权重重采样(近似),但更稳定且不改变数据分布。

核心要点

  • 代价敏感的实现:在损失函数中给每个样本按其类别加权,L=−Σᵢw_yᵢ·ℓ(yᵢ,ŷᵢ),其中 w_c 通常取 N/(K·N_c)(N 为总样本数、K 为类别数、N_c 为类别 c 的样本数)使各类的总权重相等。 与重采样的等价性 :对少数类样本重复采样(过采样)r 倍,等价于在损失中给该样本权重 r——两者都提高少数类的总损失贡献。 但两者不完全等价 :① 数据分布 ——重采样改变了经验分布(过采样使少数类占比上升),而类权重保持原始分布(只是调整损失权重);这影响模型的 校准 (重采样后的模型概率偏向少数类,需重新校准)与 方差 (过采样引入重复样本的相关性)。② 稳定性 ——类权重不引入重复样本,方差更小、训练更稳定。③ 计算成本 ——重采样改变了有效样本数(影响训练时间与 batch 组成),类权重不改变。因此 实践中优先用类权重 。
  • 扩展与要点:① Focal Loss ——ℓ_FL=−α(1−p_t)^γ log p_t,其中 (1−p_t)^γ 降低'易分类样本'的权重(p_t 大则权重小),使模型聚焦难样本。它同时处理了 类别不平衡 (α 平衡类间)与 难易样本不平衡 (γ 聚焦难样本),是目标检测(RetinaNet)的核心。② 代价矩阵的一般形式 ——若不同误判有不同代价(而非只按类别),可直接在损失中用代价矩阵 C_ij(把 i 判为 j 的代价)加权,这是最一般的代价敏感学习。③ 类权重 vs 重采样的选择 ——数据量充足时用类权重(稳定);数据极少时重采样(尤其过采样)可能更有效(因为增加了实际样本的多样性,尽管是合成的)。④ 校准问题 ——代价敏感训练后模型的输出概率不再反映真实频率(因为损失被重新加权),若需概率解释应先校准(或用'先验校正'公式调整)。⑤ 与阈值调整的关系 ——代价敏感改变模型参数,阈值调整改变决策规则;两者可结合(先代价敏感训练,再按验证集调阈值)。⑥ 评估 ——用代价加权的指标(如期望代价)而非 accuracy 评估。
  • 类权重 = 重采样比例的近似
  • Focal loss 进一步降低易样本权重

常见陷阱

  • 认为类权重与重采样完全等价(校准与方差不同)
  • 代价敏感训练后不校准就把概率当真实频率