LightGBM和XGBoost使用scale_pos_weight处理不平衡数据源码分析

lightGBM和XGBoost都提供了 scale_pos_weight 参数来处理正样本和负样本的不平衡问题。
scale_pos_weight=/ s c a l e _ p o s _ w e i g h t = 负 样 本 数 / 正 样 本 数

LightGBM

source

// weight for label
label_weights_[0] = 1.0f;
label_weights_[1] = 1.0f;
// if using unbalance, change the labels weight
if (is_unbalance_ && cnt_positive > 0 && cnt_negative > 0) {
  if (cnt_positive > cnt_negative) {
    label_weights_[1] = 1.0f;
    label_weights_[0] = static_cast<double>(cnt_positive) / cnt_negative;
  } else {
    label_weights_[1] = static_cast<double>(cnt_negative) / cnt_positive;
    label_weights_[0] = 1.0f;
  }
}
label_weights_[1] *= scale_pos_weight_;

可以看到lightGBM通过增加正样本标签的权重,即label_weights_[1] *= scale_pos_weight_;来处理样本不平衡的问题

XGBoost

source

for (omp_ulong i = 0; i < n - remainder; i += 8) {
  avx::Float8 y(&info.labels_[i]);
  avx::Float8 p = Loss::PredTransform(avx::Float8(&preds_h[i]));
  avx::Float8 w = info.weights_.empty() ? avx::Float8(1.0f)
                                       : avx::Float8(&info.weights_[i]);
  // Adjust weight
  w += y * (scale * w - w);
  avx::Float8 grad = Loss::FirstOrderGradient(p, y);
  avx::Float8 hess = Loss::SecondOrderGradient(p, y);
  avx::StoreGpair(gpair_ptr + i, grad * w, hess * w);
}

可以看到XGBoost使用增大CART树叶子的分数w,即w += y * (scale * w - w);来处理样本不平衡的问题。
之前把w误理解成线性回归里wx+b的w了,一直想不明白增加w的值怎么达到效果-_-#

  • 3
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值