xgboost 中的多类分类(python) [英] multiclass classification in xgboost (python)

查看:289
本文介绍了xgboost 中的多类分类(python)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何使用目标函数multi:softmax"将类数或评估指标传递给 xgb.XGBClassifier.

I can't figure out how to pass number of classes or eval metric to xgb.XGBClassifier with the objective function 'multi:softmax'.

我查看了许多文档,但只讨论了接受 n_class/num_class 的 sklearn 包装器.

I looked at many documentations but the only talk about the sklearn wrapper which accepts n_class/num_class.

我当前的设置看起来像

kf = cross_validation.KFold(y_data.shape[0], \
    n_folds=10, shuffle=True, random_state=30)
err = [] # to hold cross val errors
# xgb instance
xgb_model = xgb.XGBClassifier(n_estimators=_params['n_estimators'], \
    max_depth=params['max_depth'], learning_rate=_params['learning_rate'], \
    min_child_weight=_params['min_child_weight'], \
    subsample=_params['subsample'], \
    colsample_bytree=_params['colsample_bytree'], \
    objective='multi:softmax', nthread=4)

# cv
for train_index, test_index in kf:
    xgb_model.fit(x_data[train_index], y_data[train_index], eval_metric='mlogloss')
    predictions = xgb_model.predict(x_data[test_index])
    actuals = y_data[test_index]
    err.append(metrics.accuracy_score(actuals, predictions))

推荐答案

您无需在 scikit-learn API 中为 XGBoost 分类设置 num_class.当 fit 被调用时它会自动完成.查看 xgboost/sklearn.pyXGBClassifierfit方法的开头:

You don't need to set num_class in the scikit-learn API for XGBoost classification. It is done automatically when fit is called. Look at xgboost/sklearn.py at the beginning of the fit method of XGBClassifier:

    evals_result = {}
    self.classes_ = np.unique(y)
    self.n_classes_ = len(self.classes_)

    xgb_options = self.get_xgb_params()

    if callable(self.objective):
        obj = _objective_decorator(self.objective)
        # Use default value. Is it really not used ?
        xgb_options["objective"] = "binary:logistic"
    else:
        obj = None

    if self.n_classes_ > 2:
        # Switch to using a multiclass objective in the underlying XGB instance
        xgb_options["objective"] = "multi:softprob"
        xgb_options['num_class'] = self.n_classes_

这篇关于xgboost 中的多类分类(python)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆