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

XGBoost Early Stopping Rounds报错:fit()不识别该参数

TypeError: XGBModel.fit() got an unexpected keyword argument 'early_stopping_rounds' 解决方案

核心原因

XGBoost的scikit-learn接口(如XGBRegressor)在较新版本中,已弃用将early_stopping_rounds直接作为fit()方法参数的写法,转而要求通过callbacks参数传入官方回调类实现早停逻辑。

修复代码

将原fit()方法中的early_stopping_rounds=50替换为callbacks参数,使用xgb.callback.EarlyStopping回调:

model.fit(
    X_train,
    y_train,
    eval_set=[(X_valid, y_valid)],
    verbose=False,
    callbacks=[xgb.callback.EarlyStopping(rounds=50, verbose=False)]
)

完整修改后的目标函数

def objective(trial):
    # Suggest values for hyperparameters
    params = {
        "objective": "reg:squarederror",
        "eval_metric": "rmse",
        "tree_method": "hist",  # Use hist method
        "device": "cuda",       # Specify using GPU
        "learning_rate": trial.suggest_float("learning_rate", 0.01, 0.3, log=True),
        "max_depth": trial.suggest_int("max_depth", 3, 10),
        "min_child_weight": trial.suggest_float("min_child_weight", 1, 10),
        "gamma": trial.suggest_float("gamma", 0, 1),
        "subsample": trial.suggest_float("subsample", 0.5, 1.0),
        "colsample_bytree": trial.suggest_float("colsample_bytree", 0.5, 1.0),
        "lambda": trial.suggest_float("lambda", 1e-3, 10.0, log=True),
        "alpha": trial.suggest_float("alpha", 1e-3, 10.0, log=True),
        "n_estimators": 1000  # Define n_estimators in the initialization of the model
    }

    # Initialize the model
    model = xgb.XGBRegressor(**params)

    # Train the model with early stopping callback
    model.fit(
        X_train,
        y_train,
        eval_set=[(X_valid, y_valid)],
        verbose=False,
        callbacks=[xgb.callback.EarlyStopping(rounds=50, verbose=False)]
    )

    # Predict and calculate RMSE for validation set
    preds = model.predict(X_valid)
    rmse = mean_squared_error(y_valid, preds, squared=False)

    return rmse  # Optuna minimizes this

额外排查点

  • 版本验证:运行print(xgb.__version__)确认版本≥1.6.0(回调方式为该版本后推荐用法)。
  • 环境冲突检查:避免conda与pip混合安装XGBoost,可通过pip show xgboost或conda list xgboost确认实际运行版本。
  • 旧版本兼容:若需保留early_stopping_rounds参数,需回退到XGBoost 1.5.x版本,但不推荐(旧版本存在已知bug)。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 06:37:09