Which model: Best estimator from gridsearchCV or all training data?

Viewed 2485

I am a little confused when it comes to gridsearch and fitting the final model. I split the in 2: training and testing. The testing set is only used for final evaluation. I perform grid search only using the training data.

Say one has done a grid search over several hyperparameters using cross-validation. The grid search gives the best combination of the hyperparameters. Next step is to train the model, and this is where I am confused. I see 2 possibilities:

1) Don't train the model. Use the parameters from the best model from the grid search.

or

2) Don't use the parameters from the best model from the grid search. Train the model on the full training set with the best hyperparameter combination from the grid search.

What is the correct approach, 1 or 2?

2 Answers

This is probably late, but might be useful for someone else who comes along.

GridSearchCV has an attribute called refit, which is set to True by default. This means that after performing k-fold cross-validation (i.e., training on a subset of the data you passed in), it refits the model using the best hyperparameters from the grid search, on the complete training set.

Presumably your question, from what I can glean, can be summarized as:

Suppose you use 5-fold cross-validation. Your model is then fitted only on 4 folds, as the fifth fold is used for validation. So would you need to retrain the model on the whole of train (i.e., the data from all 5 folds)?

The answer is no, provided you set refit to True, in which case GridSearchCV will perform the training over the whole of the training set using the best hyperparameters it has found after cross-validation. It will then return the trained estimator object, on which you can directly call the predict method, as you would normally do otherwise.

Refer: https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.GridSearchCV.html

You train the model using the training set and the parameters obtained by the GridSearch.

And then you can test the model with the test set.

Related