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

Scikit-Learn:调用GridSearchCV的.fit()后出现意外输出问题

GridSearchCV with XGBRegressor Showing Unspecified Default Parameters After .fit()

Let's break down what's likely happening here and how to fix it — this is a common pitfall when working with GridSearchCV in scikit-learn!

First: The Most Probable Culprit — You're Looking at the Wrong Model Instance

When you initialize xgb_model = xgb.XGBRegressor() and pass it to GridSearchCV, the grid search doesn't modify this original instance. Instead, it creates new XGBRegressor instances for every parameter combination in your params dict during cross-validation.

If you're checking the parameters of the original xgb_model after calling grid_search_cv.fit(X, y), you'll still see the default values — that's expected! The tuned, optimal model lives inside the GridSearchCV object itself, not your initial model variable.

To see the parameters of your best-performing model:

# Print the best parameter combination found
print("Best hyperparameters:", grid_search_cv.best_params_)

# Inspect the full tuned model
best_model = grid_search_cv.best_estimator_
print("Best model parameters:", best_model.get_params())

This should show you the parameters from your params grid that gave the best cross-validation score.

Other Things to Verify

  1. Double-Check Parameter Names
    Make sure the keys in your params dict exactly match the parameter names accepted by XGBRegressor (scikit-learn interface). Your current params look correct (max_depth, learning_rate, n_estimators, etc.), but typos (like max_depthh or learningrate) would cause GridSearchCV to fall back to default values for misspelled parameters.

  2. Understand the Difference Between Model Params vs. Fit Params
    You mentioned not finding .fit() docs for GridSearchCV — let's clarify:

    • Parameters in your params dict are passed to the XGBRegressor constructor (these are model initialization settings like tree depth or learning rate).
    • If you need to pass parameters to the model's .fit() method (like eval_set for early stopping, sample_weight, etc.), you do this via the fit_params argument in grid_search_cv.fit(). For example:
      fit_params = {
          "eval_set": [(X_val, y_val)],
          "early_stopping_rounds": 50,
          "verbose": 0
      }
      grid_search_cv.fit(X, y, **fit_params)
      

    Your current code doesn't need this, but it's useful to know for more advanced tuning.

  3. Confirm GridSearchCV is Actually Tuning
    The verbose=2 setting in your GridSearchCV should print progress updates showing which parameter combinations it's testing. If you're not seeing these logs, that might indicate an issue with how the grid is being set up (though your code looks correct here).

Quick Recap

  • Stop checking the original xgb_model — use grid_search_cv.best_params_ and grid_search_cv.best_estimator_ to access your tuned model.
  • Verify all parameter names in your params dict match XGBRegressor's scikit-learn interface.
  • Use fit_params if you need to pass arguments to the model's .fit() method (not constructor params).

This should resolve the confusion of seeing default parameters — your grid search is almost certainly working as intended, you just were looking in the wrong place!

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.15 04:51:05