XGBClassifier num_class is invalid

梦想的初衷 提交于 2020-06-10 17:45:46

问题


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'

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

xgb.XGBClassifier(params, num_class=100)

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!


回答1:


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_

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



来源:https://stackoverflow.com/questions/35384977/xgbclassifier-num-class-is-invalid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!