Invalid XGBClassifier class value

I use XGBClassifier (in xgboost) to classify multiple classes. After running the classifier, I get an error message:

unexpected keyword argument 'num_class' 

The code that caused this error is shown below (params is a valid set of parameters for xgb):

 xgb.XGBClassifier(params, num_class=100) 

I searched a bit and found that the num_class parameter has the name 'n_classes' to implement the XGBClassifier script. I tried this change and got a similar error:

 unexpected keyword argument 'n_classes' 

The code that caused this error is shown below:

 xgb.XGBClassifier(params, num_class=100) 

Any help in fixing this error is appreciated!

+6
source share
2 answers

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

 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 out the full source code here: https://github.com/dmlc/xgboost/blob/master/python-package/xgboost/sklearn.py

+6
source

Bishvarup Bhattacarji's answer resolved my problem. I wanted to say thanks, as well as rephrase the answer, as it related to my situation:

I used GridSearchCV to optimize hyperparameters for xgboost. This worked well for a problem with 3 classes, but when I went over to the problem with 2 classes, I saw the error cited by OP. My mistake was to specify a fixed value for 'target' as ["multi: softprob"], mistakenly assuming that 2 classes would be covered by "multi". The simple removal of this variable from the search parameters in the grid allowed the XGBClassifier to adapt to a given number of classes. Unfortunately, the error is no more informative. In my case, it should be said that multi: softprob is not suitable for the purpose when there are 2 classes.

I would just add a comment to this answer, but I have no points for this.

0
source

All Articles