Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XGBClassifier num_class is invalid

Tags:

python

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'

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!

like image 434
Eric Broda Avatar asked Feb 13 '16 20:02

Eric Broda


1 Answers

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

like image 144
Bishwarup Bhattacharjee Avatar answered Sep 19 '22 06:09

Bishwarup Bhattacharjee