如何在 xgboost 中为不平衡数据设置多类分类的权重?

数据挖掘 xgboost 多类分类 阶级失衡
2022-02-13 05:17:17

这篇文章中,我知道您可以设置scale_pos_weight不平衡的数据集。但是,对于不平衡数据集中的多分类问题,我不太明白如何在dmatrix.

如何将 XGBoost 用于多分类问题中的不平衡数据集?

1个回答

正如您所说,scale_pos_weight适用于两个类(二元分类)。weight可用于三个或更多类。参数进入xgb.DMatrix函数并且必须包含每个观察值的一个值。

例子:

library(xgboost)
data(iris)

# We'll predict Species
label = as.integer(iris$Species)-1
iris$Species = NULL

# Split the data for training and testing (75/25 split)
n = nrow(iris)
train.index = sample(n,floor(0.75*n))

# For example, pick a weight of 1.5 for label "0", 1.0 for the other Species
weights = sapply(label[train.index], function(x) {ifelse(x == 0, 1.5, 1.0)})

# Train the data using weights
xgb.train = xgb.DMatrix(data=as.matrix(iris[train.index,]), label=label[train.index], weight = weights)

可以在此处找到类似的问题。