Grid search in Python
GridSearchCV tries every combination with cross validation and keeps the best.
from sklearn.datasets import load_iris
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = load_iris(return_X_y=True)
pipeline = Pipeline([("scale", StandardScaler()), ("svc", SVC())])
grid = {
"svc__C": [0.1, 1.0, 10.0],
"svc__kernel": ["linear", "rbf"],
}
search = GridSearchCV(pipeline, grid, cv=5, scoring="accuracy", n_jobs=1)
search.fit(X, y)
print(search.best_params_)
print(f"{search.best_score_:.3f}")
print(search.best_estimator_.named_steps["svc"].C)
How it works
- Parameter names use
step__parameterinside a pipeline. best_params_andbest_score_report the winner.refit=Trueleaves the best model fitted on all the data.
Keywords and builtins used here
print
The run, in numbers
- Lines
- 19
- Characters to type
- 583
- Tokens
- 160
- Three-star pace
- 110 tpm
At the three-star pace of 110 tokens a minute, this run takes about 87 seconds.
Step 3 of 4 in Validation, step 10 of 25 in Machine learning with scikit-learn.