我正在使用 XGBClassifier 对不平衡的多类目标进行建模。我有几个问题:
First I would like to now where should I use the parameter weight on the instantion of the classifier or on the fit step of the pipeline?
Second question is how I calculate a weights. I assume that the sum of the array should be 1.
Third: Is there any order of the weight array that maps the diferent label classes?
谢谢大家
对于你的第一个问题:
我应该在哪里使用参数权重
使用sample_weight
中XGBClassifier.fit()
xgb_clf = xgb.XGBClassifier()
xgb_clf.fit(X, y, sample_weight=sample_weight)
使用时pipeline
:
pipe = Pipeline([
('my_xgb_clf', xgb.XGBClassifier()),
])
pipe.fit(X, y, my_xgb_clf__sample_weight=sample_weight)
顺便说一句,sklearn 中的某些 API 不支持sample_weight
kwarg,例如learning_curve
.
所以我只是这样做:
import functools
xgb_clf.fit = functools.partial(xgb_clf.fit, sample_weight=sample_weight)
注意:您需要fit()
在网格搜索后再次修补,因为GridSearchCV.best_estimator_
不会是原始估计器。
对于第二个问题:
我如何计算权重。我假设数组的总和应该是 1。
from sklearn.utils import compute_sample_weight
sample_weight = compute_sample_weight('balanced', y_train)
这class_weight='balanced'
在 sklearn 中模拟。
笔记:
class_weight='balanced_subsample'
我找不到模拟这个的方法。对于第三个问题:
有什么顺序吗...
抱歉我没明白你的意思...
也许你想要订单xgb_clf.classes_
?您可以在调用后访问它xgb_clf.fit
。或者只是使用np.unique(y_train)
.
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句