GridSearchCV
- Machine Learning course
- Intermediate
- Lesson 20/112
- 5 min
GridSearchCV combines grid search and cross-validation.
GridSearchCV
GridSearchCV(estimator, param_grid, cv=...) trains every combination in every fold, picks the best by mean score and (with the default refit=True) retrains it on all the training data:
gs = GridSearchCV(pipe, {"svc__C": [0.1, 1, 10]}, cv=5, scoring="f1", n_jobs=-1)
gs.fit(X_train, y_train)
print(gs.best_params_)
print(gs.score(X_test, y_test)) # test with the best modelscoring sets the metric used for the selection; the default is the estimator's score method.
Several metrics
scoring can be a list or a dict. In that case refit must name the metric used for the selection:
gs = GridSearchCV(pipe, izgara, cv=5,
scoring={"f1": "f1", "auc": "roc_auc"},
refit="f1")
gs.fit(X_train, y_train)
print(gs.cv_results_["mean_test_auc"])That way the selection is made by F1 while the AUC values can be reported too.
Practice Run the grid
Run GridSearchCV(..., cv=5) on pipe with the grid {"svc__C": [0.1, 1, 10], "svc__gamma": [0.01, 0.1]}. Print best_params_ and the test accuracy (round(..., 3)) on separate lines.
Starter code
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
pipe = Pipeline([("olcek", StandardScaler()), ("svc", SVC())])
# buraya yazSample solution
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = load_breast_cancer(return_X_y=True)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=0, stratify=y)
pipe = Pipeline([("olcek", StandardScaler()), ("svc", SVC())])
gs = GridSearchCV(pipe, {"svc__C": [0.1, 1, 10], "svc__gamma": [0.01, 0.1]}, cv=5)
gs.fit(X_train, y_train)
print(gs.best_params_)
print(round(gs.score(X_test, y_test), 3))Expected output
{'svc__C': 10, 'svc__gamma': 0.01}
0.972
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What happens if <code>refit=False</code> is given?
- No search is done
- The test set is used
- It errors
- The best parameters are found but the model isn't retrained; gs.predict can't be used Correct answer
Explanation: refit=True is the default and produces best_estimator_.
2 Make the selection by F1.
gs = GridSearchCV(pipe, izgara, cv=5, ____="f1")
gs = GridSearchCV(pipe, izgara, cv=5, scoring="f1")
Explanation: scoring takes a single metric name or a dict of metrics.
3 Match the parameters.
| Term | Meaning |
|---|---|
param_grid |
Values to try |
cv |
Number of folds |
scoring |
Selection metric |
refit |
Retrain the best |