LightGBM中class_weight和scale_pos权重的区别

数据挖掘 分类 阶级失衡 lightgbm
2021-09-28 18:37:15

我有一个非常不平衡的数据集,正样本与负样本的比率为 1:496。评分指标是 f1 分数,我想要的模型是 LightGBM。我正在使用 LightGBM 的 sklearn 实现。

我已阅读有关class_weightLightGBM 参数的文档:

class_weight : dict, 'balanced' or None, optional (default=None) 与 {class_label: weight} 形式的类关联的权重。该参数仅用于多类分类任务;对于二元分类任务,您可以使用 is_unbalance 或 scale_pos_weight 参数。“平衡”模式使用 y 的值自动调整与输入数据中的类频率成反比的权重,如 n_samples / (n_classes * np.bincount(y))。如果没有,所有的类都应该有一个权重。请注意,如果指定了 sample_weight,这些权重将与 sample_weight(通过 fit 方法传递)相乘。

在我的数据集上使用class_weight参数时,这是一个二元分类问题,我得到的分数(0.7899)比使用推荐scale_pos_weight参数(0.2388)时要好得多。我应该使用class_weight参数还是scale_pos_weight参数来平衡类?

1个回答

您可以通过使用class_weight,scale_pos_weightis_unbalanced对不平衡数据集进行二元分类来获得相同的结果。

环境

class_weight = {0: (number of negative samples / number of positive samples), 
                1: (number of positive samples / number of negative samples)}

与设置is_unbalance = True或相同scale_pos_weight = (no. of negative samples / number of positive samples)