Tune With GridSearchCV
Searching hyperparameters safely.
Tune With GridSearchCV is a free Data Science Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Data Science Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Settings You Choose
Some model settings are not learned from data; you pick them before training. These are hyperparameters, and good choices matter a lot.
Guessing Is Slow
Tweaking one value, re-running, and eyeballing the score by hand wastes time and misses better combos. Let a search do it. 🔍
Enter GridSearchCV
GridSearchCV tries every combination in a grid you define and uses cross-validation to score each one fairly.
from sklearn.model_selection import GridSearchCVDefine the Grid
You list each hyperparameter and the values to try in a dictionary. Every value pairing becomes one candidate to test.
param_grid = {'C': [0.1, 1, 10]}Tune Inside a Pipeline
To target a pipeline step, prefix the name with the step plus a double underscore. This naming points the search at the right knob.
param_grid = {'model__C': [0.1, 1, 10]}Run the Search
Wrap your estimator and grid, then call fit. It trains and scores every combination across the folds for you.
search = GridSearchCV(pipe, param_grid, cv=5)
search.fit(X_train, y_train)Read the Winner
After fitting, best_params_ tells you which combination won, and best_score_ shows its average cross-validated score.
print(search.best_params_, search.best_score_)Predict With the Best
The search refits the top combo on all training data. Just call predict on the search object to use that best_estimator.
preds = search.predict(X_test)Pick the Right Scorer
By default it optimizes accuracy. Set scoring to f1 or roc_auc so the search chases the metric your problem cares about.
GridSearchCV(pipe, param_grid, scoring='f1', cv=5)Mind the Cost
The grid grows fast: combinations times folds equals fits. When it explodes, try RandomizedSearchCV to sample instead.
Leak-Free by Design
Because the whole pipeline is searched, prep refits inside each fold. Tuning stays honest with zero leakage sneaking in.
Quick Check
To tune a pipeline step named model, how do you key the parameter?
Recap
You can now let GridSearchCV test parameter combos with cross-validation and hand you the best one. Next, saving your trained pipeline. 💾
Frequently asked questions
Is the “Tune With GridSearchCV” lesson free?
Yes — the full text of “Tune With GridSearchCV” is free to read here on the web, and the Data Science Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Data Science Academy course, upgrade to CoddyKit PRO.
What will I learn in “Tune With GridSearchCV”?
Searching hyperparameters safely. You practise Data Science Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Data Science Academy?
No prior experience is required. Data Science Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Tune With GridSearchCV” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Data Science Academy lesson?
Yes. Every Data Science Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.