Scikit-Learn:调用GridSearchCV的.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
Double-Check Parameter Names
Make sure the keys in yourparamsdict exactly match the parameter names accepted byXGBRegressor(scikit-learn interface). Your current params look correct (max_depth,learning_rate,n_estimators, etc.), but typos (likemax_depthhorlearningrate) would cause GridSearchCV to fall back to default values for misspelled parameters.Understand the Difference Between Model Params vs. Fit Params
You mentioned not finding.fit()docs for GridSearchCV — let's clarify:- Parameters in your
paramsdict 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 (likeeval_setfor early stopping,sample_weight, etc.), you do this via thefit_paramsargument ingrid_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.
- Parameters in your
Confirm GridSearchCV is Actually Tuning
Theverbose=2setting 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— usegrid_search_cv.best_params_andgrid_search_cv.best_estimator_to access your tuned model. - Verify all parameter names in your
paramsdict match XGBRegressor's scikit-learn interface. - Use
fit_paramsif 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

