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?
1 Answer
It doesn't, please try the following code
CELL1:
import numpy as np
from sklearn import svm, datasets
from sklearn.model_selection import GridSearchCV
iris = datasets.load_iris()
CELL2:
%%time
parameters = {'kernel':('linear', 'rbf'), 'C':np.linspace(0.1,100,1000)}
svc = svm.SVC()
clf = GridSearchCV(svc, parameters)
clf.fit(iris.data, iris.target)
CELL3:
%%time
clf.best_params_
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.
best_params_
gets populated when fitting, in theBaseSearchCV
class (whichGridSearchCV
inherits from), so indeed it should take basically 0 time to retrieve. $\endgroup$