You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Optuna调优XGBoost的n_estimators时出现未使用警告

问题原因与解决办法

警告原因

你看到的警告是因为**n_estimators不是XGBoost原生xgb.train()接口的合法参数**。这个参数属于XGBoost的scikit-learn兼容接口(如XGBClassifier),而原生训练接口是通过num_boost_round参数来控制决策树数量(即迭代次数)的。你把n_estimators放进了传给xgb.train()的params字典里,XGBoost无法识别该参数,因此抛出“未被使用”的警告。

另外你的代码存在冗余问题:learning_rate和eta是同一参数的不同名称(eta是learning_rate的别名),同时设置属于重复定义,建议保留其中一个即可。

解决步骤

  1. 从参数字典中移除n_estimators
    删除params字典里的'n_estimators': trial.suggest_int(...)条目,单独在外部生成该参数。

  2. 单独定义n_estimators并传入num_boost_round
    在params字典外生成n_estimators参数,调用xgb.train()时将其传给num_boost_round参数。

  3. 移除重复的learning_rate或eta
    保留其中一个参数即可,推荐保留learning_rate,命名更直观。

修改后的代码示例

def objective(trial):
    # Define the search space for hyperparameters
    params = {
        'objective': 'binary:logistic',  # For binary classification
        'eval_metric': 'auc',  # AUC-ROC as the evaluation metric
        'booster': 'gbtree',
        'learning_rate': trial.suggest_float('learning_rate', 0.01, 0.3),
        'max_depth': trial.suggest_int('max_depth', 3, 10),
        'min_child_weight': trial.suggest_int('min_child_weight', 1, 10),
        'subsample': trial.suggest_float('subsample', 0.4, 1.0),
        'colsample_bytree': trial.suggest_float('colsample_bytree', 0.55, 1.0),
        'lambda': trial.suggest_float('lambda', 1e-5, 1.0), 
        'alpha': trial.suggest_float('alpha', 1e-5, 1.0),  
        'gamma': trial.suggest_float('gamma', 0.0, 1.0),  
    }
    # 单独定义n_estimators参数
    n_estimators = trial.suggest_int('n_estimators', 100, 1300)

    X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=42)

    dtrain = xgb.DMatrix(X_train, label=y_train)
    dval = xgb.DMatrix(X_val, label=y_val)

    # 将n_estimators传入num_boost_round
    model = xgb.train(params, dtrain, num_boost_round=n_estimators, evals=[(dval, 'eval')], verbose_eval=False)
    y_pred = model.predict(dval)

    auc_roc = roc_auc_score(y_val, y_pred)  # AUC-ROC score

    return -auc_roc

内容的提问来源于stack exchange,提问作者jerrycalebj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 17:33:13