Summary - Grid search
- Machine Learning course
- Intermediate
- Lesson 24/112
- 6 min
In this group you learned GridSearchCV and hyperparameter tuning.
Summary: grid search
In this group:
- Hyperparameters are searched with CV, only on the training set; grids are built logarithmically.
GridSearchCV: Cartesian product,scoring,refit,n_jobs.- Conditional grids with a list of dicts; counting with
ParameterGrid. best_params_,best_estimator_,cv_results_; an optimum on the edge signals that the grid should be extended.best_score_is optimistic;RandomizedSearchCVfor large searches.
Practice Model selection in one search
For Pipeline([("olcek", StandardScaler()), ("model", SVC())]), try the "model" step among [SVC(), LogisticRegression(max_iter=5000), KNeighborsClassifier()] in the grid. Print the class name of the model chosen by GridSearchCV(..., cv=5) (type(gs.best_params_["model"]).__name__) and the test accuracy with 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
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
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)
# 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
from sklearn.linear_model import LogisticRegression
from sklearn.neighbors import KNeighborsClassifier
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()), ("model", SVC())])
gs = GridSearchCV(pipe, {"model": [SVC(), LogisticRegression(max_iter=5000), KNeighborsClassifier()]}, cv=5)
gs.fit(X_train, y_train)
print(type(gs.best_params_["model"]).__name__)
print(round(gs.score(X_test, y_test), 3))Expected output
SVC 0.958
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 With the same budget, which is usually more efficient for searching a large 5-parameter space?
- A full GridSearchCV
- Manual trial
- Searching on the test set
- RandomizedSearchCV Correct answer
Explanation: Random search tries more distinct values of the important parameters.
2 Run the search on all cores.
gs = GridSearchCV(pipe, izgara, cv=5, ____=-1)
gs = GridSearchCV(pipe, izgara, cv=5, n_jobs=-1)
Explanation: n_jobs=-1 uses all processors.
3 Match the tools.
| Term | Meaning |
|---|---|
GridSearchCV |
All combinations |
RandomizedSearchCV |
n_iter samples |
ParameterGrid |
List of combinations |
cv_results_ |
Detailed results |