在 XGBoost 中,如何更改 eval 函数并保持相同的目标?

数据挖掘 Python 回归 xgboost 评估
2021-09-25 08:11:51

我想将目标保持为“reg:linear”和eval_metric定制的 RMSE,如下所示:

def customised_rmse(preds, dtrain):
    N = len(preds)
    preds = np.array(preds)
    actual = np.array(dtrain.get_label())
    crmse = np.sqrt(np.sum(np.power((preds+1)*1.0/(actual+1) - 1.1, 2))/N)
    return "custom-rmse", crmse

当我跑的时候,训练功能如下->

model = xgb.train(param_list, xgb_train, num_rounds, watchlist, None, customised_rmse, early_stopping_rounds=30)

我得到的输出是这个->

[0] train-rmse:15.1904  val-rmse:15.2102    train-custom-rmse:0.607681  val-custom-rmse:0.610993
Multiple eval metrics have been passed: 'val-custom-rmse' will be used for early stopping.
Will train until val-custom-rmse hasn't improved in 30 rounds.
[1] train-rmse:14.4936  val-rmse:14.5103    train-custom-rmse:0.588831  val-custom-rmse:0.589902

等等...

我的问题是,它正在使用“rmse”或“custom-rase”或两者进行优化?我必须做些什么来删除“rmse”,因为它默认带有“reg:linear”?

1个回答

如果您使用关键字参数,则更容易理解:

model = xgb.train(params=param_list, dtrain=xgb_train, 
        num_boost_round=num_rounds, evals=watchlist, obj=None, 
        feval=customised_rmse, early_stopping_rounds=30)

obj可以是目标函数。feval可以是自定义的评价函数。

rmse 被用来最小化训练数据的错误。然而,验证数据集上的 custom-rmse 将定义模型何时停止训练。

更改obj以更改默认值。