Search code examples
pythonpython-3.xxgboost

Why do I receive this FutureWarning when I use xgboost?


I'm using xgboost to train a binary classifier with script

class_weights = list(class_weight.compute_class_weight('balanced',np.unique(y_train),y_train))
w_array = np.ones(len(y_train), dtype='float')
for i, val in enumerate(y_train):
    w_array[i] = class_weights[val]    

eval_set = [(x_train, y_train), (x_val, y_val)]
model = XGBClassifier(max_depth=5,n_estimators=1000)
model.fit(x_train, 
          y_train,
          verbose=0,
          eval_set=eval_set,
          eval_metric='auc',
          sample_weight=w_array,
          early_stopping_rounds=200)

In the above script, x_train and x_val are arrays of shapes (386, 72) and (387, 72), respectively. y_train and y_val are arrays of zeros and ones. Running the script, I will receive a warning

FutureWarning: Pass classes=[0 1], y=[1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 0 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 0 1 1 1 1 1 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 1 1 1 1 1 1 1 1 1 1 0 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1] as keyword args. From version 0.25 passing these as positional arguments will result in an error
  FutureWarning)

What does this mean?

My xgboost version is 0.81 .


Solution

  • This refers to compute_class_weight. It asks you to explicitly pass the classes you try to predict.

    From the documentation:

    classes : ndarray

    Array of the classes occurring in the data, as given by np.unique(y_org) with y_org the original class labels.