Ottimizzare con GridSearchCV
Cercare gli iperparametri in sicurezza.
Ottimizzare con GridSearchCV è una lezione Data Science Academy gratuita su CoddyKit. Questa è la lezione 3 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Data Science Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Data Science Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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. 💾
Domande Frequenti
La lezione «Ottimizzare con GridSearchCV» è gratuita?
Sì — il testo completo di «Ottimizzare con GridSearchCV» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Data Science Academy, passa a CoddyKit PRO. Il corso Data Science Academy include 4 lezioni in totale.
Cosa imparerò in «Ottimizzare con GridSearchCV»?
Cercare gli iperparametri in sicurezza. Eserciti Data Science Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Data Science Academy?
Non è richiesta alcuna esperienza precedente. Data Science Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 3 di 4.
Quanto tempo richiede la lezione «Ottimizzare con GridSearchCV»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Data Science Academy?
Sì. Ogni lezione Data Science Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- Perché le pipeline superano i passaggi manuali
- ColumnTransformer per tipi misti
- Ottimizzare con GridSearchCV
- Salvare e ricaricare una pipeline addestrata