Ajustar com GridSearchCV
Pesquisando hiperparâmetros com segurança.
Ajustar com GridSearchCV é uma aula grátis de Data Science Academy no CoddyKit. Esta é a aula 3 de 4. Você pode ler a aula completa abaixo gratuitamente — depois pratica ao vivo no navegador com um editor de código integrado e um tutor de IA 24/7. Faz parte do caminho de aprendizado de Data Science Academy, e seu progresso é sincronizado entre a web e o app CoddyKit. O curso de Data Science Academy inclui 4 aulas no total.
Partes desta aula ainda não foram traduzidas e aparecem em inglês.
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. 💾
Perguntas Frequentes
A aula “Ajustar com GridSearchCV” é grátis?
Sim — o texto completo de “Ajustar com GridSearchCV” é grátis para ler aqui na web. Para praticá-la interativamente (um editor de código integrado e um tutor de IA 24/7) e desbloquear o restante do curso de Data Science Academy, atualize para CoddyKit PRO. O curso de Data Science Academy inclui 4 aulas no total.
O que vou aprender em “Ajustar com GridSearchCV”?
Pesquisando hiperparâmetros com segurança. Você pratica Data Science Academy com código prático que executa diretamente no navegador, e um tutor de IA 24/7 responde suas dúvidas enquanto trabalha na aula.
Preciso ter experiência prévia para começar Data Science Academy?
Nenhuma experiência prévia é necessária. Data Science Academy no CoddyKit é estruturado para alunos iniciantes até avançados, então você pode começar aqui ou desde o início e aprender no seu ritmo. Esta é a aula 3 de 4.
Quanto tempo leva a aula “Ajustar com GridSearchCV”?
A maioria das aulas CoddyKit leva cerca de 5–10 minutos. Cada uma é compacta e interativa, então você faz progresso constante e retoma exatamente de onde parou entre web e app.
Posso escrever e executar código nesta aula de Data Science Academy?
Sim. Cada aula de Data Science Academy inclui um editor de código integrado, então você escreve e executa código real direto no navegador e recebe feedback de IA instantaneamente — nenhuma configuração local necessária.
Todas as aulas deste curso
- Por que os pipelines superam etapas manuais
- ColumnTransformer para tipos mistos
- Ajustar com GridSearchCV
- Salvar e recarregar um pipeline treinado