Keithx
Keithx

Reputation: 3158

GridSearchCV() with empty param_grid

Am I understand correctly, that if I just want to use GridSearchCV()not for the hyperparameter model tuning but only for cross-validation on the existing base estimator, I can just use empty param_grid?:

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV

rf = RandomForestClassifier(random_state=0)

param_grid = {}

rf_cv = GridSearchCV(estimator=rf, param_grid=param_grid, cv= 3)
rf_cv.fit(X_train, y_train)

Upvotes: 1

Views: 992

Answers (1)

StupidWolf
StupidWolf

Reputation: 46978

Yes you can do that:

from sklearn import datasets
X_train,y_train = datasets.load_iris(return_X_y=True)

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import GridSearchCV
rf = RandomForestClassifier(random_state=0)
param_grid = {}
rf_cv = GridSearchCV(estimator=rf, param_grid=param_grid, cv= 3)
rf_cv.fit(X_train, y_train)

rf_cv.cv_results_
Out[32]: 
{'mean_fit_time': array([0.11061962]),
 'std_fit_time': array([0.00482304]),
 'mean_score_time': array([0.00982841]),
 'std_score_time': array([0.00150256]),
 'params': [{}],
 'split0_test_score': array([0.98]),
 'split1_test_score': array([0.94]),
 'split2_test_score': array([0.98]),
 'mean_test_score': array([0.96666667]),
 'std_test_score': array([0.01885618]),
 'rank_test_score': array([1], dtype=int32)}

Another option is to use cross_val_score :

from sklearn.model_selection import cross_val_score
cross_val_score(rf,X_train,y_train,cv=3)
array([0.98, 0.94, 0.98])

Upvotes: 1

Related Questions