0Pricing
Data Science Academy · Lección

Ajustar con GridSearchCV

Buscar hiperparámetros de forma segura.

Ajustar con GridSearchCV es una lección gratuita de Data Science Academy en CoddyKit. Esta es la lección 3 de 4. Puedes leer la lección completa abajo gratuitamente — luego la practicas en el navegador con un editor de código integrado y un tutor de IA 24/7. Forma parte de la ruta de aprendizaje de Data Science Academy, y tu progreso se sincroniza en la web y la app de CoddyKit. El curso de Data Science Academy incluye 4 lecciones en total.

Partes de esta lección aún no han sido traducidas y se muestran en 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 GridSearchCV

Define 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. 💾

Preguntas frecuentes

¿La lección «Ajustar con GridSearchCV» es gratis?

Sí — el texto completo de «Ajustar con GridSearchCV» es gratis para leer aquí en la web. Para practicarla de forma interactiva (editor de código integrado y tutor de IA 24/7) y desbloquear el resto del curso de Data Science Academy, actualiza a CoddyKit PRO. El curso de Data Science Academy incluye 4 lecciones en total.

¿Qué aprenderé en «Ajustar con GridSearchCV»?

Buscar hiperparámetros de forma segura. Practicas Data Science Academy con código real que ejecutas directamente en el navegador, y un tutor de IA 24/7 responde tus preguntas mientras trabajas en la lección.

¿Necesito experiencia previa para empezar Data Science Academy?

No se requiere experiencia previa. Data Science Academy en CoddyKit está estructurado para principiantes hasta estudiantes avanzados, así que puedes empezar aquí o desde el inicio y avanzar a tu ritmo. Esta es la lección 3 de 4.

¿Cuánto tiempo toma la lección «Ajustar con GridSearchCV»?

La mayoría de las lecciones de CoddyKit toman alrededor de 5–10 minutos. Cada una es compacta e interactiva, así que avanzas constantemente y retomas exactamente por donde dejaste en la web y la app.

¿Puedo escribir y ejecutar código en esta lección de Data Science Academy?

Sí. Cada lección de Data Science Academy incluye un editor de código integrado, así que escribes y ejecutas código real directamente en tu navegador y obtienes retroalimentación instantánea de IA — sin configuración local necesaria.

Todas las lecciones de este curso

  1. Por qué los pipelines superan a los pasos manuales
  2. ColumnTransformer para tipos mixtos
  3. Ajustar con GridSearchCV
  4. Guardar y volver a cargar un pipeline entrenado
← Volver a Data Science Academy