【问题标题】:The default evaluation metric used with the objective 'binary:logistic' was changed from 'error' to 'logloss'与目标“binary:logistic”一起使用的默认评估指标从“error”更改为“logloss”
【发布时间】:2021-10-09 09:45:22
【问题描述】:

在使用 optuna 进行超参数调整后,我正在尝试将 XGBClassifier 拟合到我的数据集,但我不断收到以下警告:

与目标“binary:logistic”一起使用的默认评估指标已从“error”更改为“logloss”

下面是我的代码:

#XGBC MODEL
model = XGBClassifier(random_state = 69)

cross_rfc_score = -1 * cross_val_score(model, train_x1, train_y,
                           cv = 5, n_jobs = -1, scoring = 'neg_mean_squared_error')
base_rfc_score = cross_rfc_score.mean()

但是如果我使用 Optuna 然后拟合获得的参数,它会给我警告。下面是代码:

def objective(trial):
    learning_rate = trial.suggest_float('learning_rate', 0.001, 0.01)
    n_estimators = trial.suggest_int('n_estimators', 10, 500)
    sub_sample = trial.suggest_float('sub_sample', 0.0, 1.0)
    max_depth = trial.suggest_int('max_depth', 1, 20)

    params = {'max_depth' : max_depth,
           'n_estimators' : n_estimators,
           'sub_sample' : sub_sample,
           'learning_rate' : learning_rate}

    model.set_params(**params)

    return np.mean(-1 * cross_val_score(model, train_x1, train_y,
                            cv = 5, n_jobs = -1, scoring = 'neg_mean_squared_error'))

xgbc_study = optuna.create_study(direction = 'minimize')
xgbc_study.optimize(objective, n_trials = 10)

xgbc_study.best_params
optuna_rfc_mse = xgbc_study.best_value

model.set_params(**xgbc_study.best_params)
model.fit(train_x1, train_y)
xgbc_optuna_pred = model.predict(test_x1)
xgbc_optuna_mse1 = mean_squared_error(test_y, xgbc_optuna_pred)

完整的警告是:

从 XGBoost 1.3.0 开始,与目标“binary:logistic”一起使用的默认评估指标从“error”更改为“logloss”。如果您想恢复旧行为,请显式设置 eval_metric。

我希望 MSE 作为我选择的指标。

【问题讨论】:

    标签: xgboost mse optuna


    【解决方案1】:

    正如here 所述,尝试将eval_metric 添加到您的.fit

    model.fit(train_x1, train_y, eval_metric='rmse')
    

    因为优化 rmsemse 会导致相同的结果。

    【讨论】:

    • 我做了更改model.fit(train_x1, train_y, eval_metric='mse'),但给了我XGBoostError: [21:13:39] ..\src\metric\metric.cc:49: Unknown metric function mse的错误
    • 因为没有 mse 指标。这就是为什么我建议rmse,因为它基本上是一样的(一个是另一个的根)
    • 如果你愿意,你可以自己定义mse,但是当你有rmse并且它只是为了优化目的时你为什么要定义(因为你以后创建xgbc_optuna_mse1呢?
    • 好的,知道了!谢谢!
    猜你喜欢
    • 1970-01-01
    • 2016-02-16
    • 1970-01-01
    • 2020-03-01
    • 2019-03-29
    • 1970-01-01
    • 1970-01-01
    • 2015-09-08
    • 2011-03-22
    相关资源
    最近更新 更多