本文介绍了XGBClassifier num_class 无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 XGBClassifier(在 xgboost 中)进行多类分类.执行分类器后,我收到一条错误消息:

I am using XGBClassifier (in xgboost) for a multi-class classification. Upon executing the classifier, I am receiving an error stating:

unexpected keyword argument 'num_class'

下面列出了导致此错误的代码(params 是 xgb 的一组有效参数):

Code that caused this error is listed below (params is a valid set of parameters for xgb):

xgb.XGBClassifier(params, num_class=100)

我搜索了一下,发现 'num_class' 参数被命名为 'n_classes' 用于 XGBClassifier 的 scikit 实现.我尝试了此更改并收到了类似的错误:

I searched a bit and found that 'num_class' parameter is named 'n_classes' for scikit implementation of XGBClassifier. I tried this change and received a similar error:

unexpected keyword argument 'n_classes'

导致此错误的代码如下:

Code that caused this error is listed below:

xgb.XGBClassifier(params, num_class=100)

对修复此错误的任何帮助表示赞赏!

Any help in fixing this error is appreciated!

推荐答案

在 Sklearn XGB API 中,您不需要明确指定 num_class 参数.如果目标超过 2 个级别,XGBClassifier 会自动切换到多类分类模式.

In the Sklearn XGB API you do not need to specify the num_class parameter explicitly. In case the target has more than 2 levels, XGBClassifier automatically switches to multiclass classification mode.

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

 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_

在这里查看完整的源代码:https://github.com/dmlc/xgboost/blob/master/python-package/xgboost/sklearn.py

Check the complete source code here: https://github.com/dmlc/xgboost/blob/master/python-package/xgboost/sklearn.py

这篇关于XGBClassifier num_class 无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-26 19:57