scikit-learn中的sample_weight与class_weight相比如何?

Kei*_*ith 4 python machine-learning scikit-learn

我想在不平衡的分类问题上使用sklearn.ensemble.GradientBoostingClassifier。我打算针对接收器工作特性曲线(ROC AUC)下的面积进行优化。为此,我想重新分配班级以使小班级对分类器更加重要。

通常,这可以通过设置class_weight =“ balanced” 来实现(例如在RandomForestClassifier中),但GradientBoostingClassifier中没有此类参数。

该文件说:

“平衡”模式使用y值自动将权重与输入数据中的类频率成反比地调整为n_samples /(n_classes * np.bincount(y))

如果y_train是目标值为{0,1}的目标数据框,则说明该文档暗示应重现class_weight =“ balanced”

sample_weight = y_train.shape[0]/(2*np.bincount(y_train))
clf = ensemble.GradientBoostingClassifier(**params)
clf.fit(X_train, y_train,sample_weight = sample_weight[y_train.values])
Run Code Online (Sandbox Code Playgroud)

这是正确的还是我错过了什么?

KPL*_*zen 9

我建议您class_weight.compute_sample_weight在scikit-learn中使用该实用程序。例如:

from sklearn.utils.class_weight import compute_sample_weight
y = [1,1,1,1,0,0,1]
compute_sample_weight(class_weight='balanced', y=y)
Run Code Online (Sandbox Code Playgroud)

输出:

array([ 0.7 ,  0.7 ,  0.7 ,  0.7 ,  1.75,  1.75,  0.7 ])
Run Code Online (Sandbox Code Playgroud)

您可以将其用作sample_weight关键字的输入。