xgb.train中自定义评价函数
Using builtin callbacks
By default, training methods in XGBoost have parameters like and /, when specified the training procedure will define the corresponding callbacks internally. For example, when is specified, callback is invoked inside iteration loop. You can also pass this callback function directly into XGBoost:early_stopping_roundsverboseverbose_evalearly_stopping_roundsEarlyStopping
D_train = xgb.DMatrix(X_train, y_train)
D_valid = xgb.DMatrix(X_valid, y_valid)
### Define a custom evaluation metric used for early stopping.
def eval_error_metric(predt, dtrain: xgb.DMatrix):
label = dtrain.get_label()
r = np.zeros(predt.shape)
gt = predt > 0.5
r[gt] = 1 - label[gt]
le = predt <= 0.5
r[le] = label[le]
return 'CustomErr', np.sum(r)
### Specify which dataset and which metric should be used for early stopping.
early_stop = xgb.callback.EarlyStopping(rounds=early_stopping_rounds,
metric_name='CustomErr',
data_name='Train')
booster = xgb.train(
{'objective': 'binary:logistic',
'eval_metric': ['error', 'rmse'],
'tree_method': 'hist'}, D_train,
evals=[(D_train, 'Train'), (D_valid, 'Valid')],
feval=eval_error_metric,
num_boost_round=1000,
callbacks=[early_stop],
verbose_eval=False)
dump = booster.get_dump(dump_format='json')
assert len(early_stop.stopping_history['Valid']['CustomErr']) == len(dump)
使用
def xgb_kappa(preds, dtrain: xgb.DMatrix):
y_true = dtrain.get_label()
preds = np.argmax(preds.reshape(3, -1), axis=0)
score = cohen_kappa_score(y_true, preds)
return 'kappa', score