So I tuned the hyperparameters using GridSearchCV, fitted the model to the data, and then used best_params_. I'm just curious why GridSearchCV takes too long to run best_params_, unlike RandomSearchCV where it instantly gives answers. The time it takes for GridSearchCV to give the best_params_ is similar to the time it takes for GridSearchCV to tune hyperparameters, and fit the model to the data. It's as if it's doing it all over again when it has done so already. Is this the case? If not, what's taking it so long when it should have saved the best_params_ when I ran GridSearchCV the first time?

  • $\begingroup$ I have never had this problem. best_params_ gets populated when fitting, in the BaseSearchCV class (which GridSearchCV inherits from), so indeed it should take basically 0 time to retrieve. $\endgroup$ – Ben Reiniger Nov 23 '20 at 2:49

It doesn't, please try the following code


import numpy as np
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
iris = datasets.load_iris()


parameters = {'kernel':('linear', 'rbf'), 'C':np.linspace(0.1,100,1000)}
svc = svm.SVC()
clf = GridSearchCV(svc, parameters)
clf.fit(iris.data, iris.target)



Wall time of CELL2 will be about 7-9 seconds. Wall time of CELL3 will be 0ns. ( instantaneous )

This is because best_params_ is an argument of GridSearchCV. It is however only created (and accessible) once you run .fit method.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.