Tuning Pipelines with GridSearchCV in scikit-learn
Introduction: Why Tune Pipelines?
Now that you have learned how to reliably evaluate your models using cross-validation, it is time to take the next step in building robust machine learning workflows. In real-world projects, you rarely use raw data directly with your models. Instead, you often need to preprocess your data — such as scaling features or encoding categories — before fitting a model. If you tune your model’s hyperparameters without including these preprocessing steps, you risk introducing data leakage or making your workflow harder to manage.
This is where pipelines come in. A pipeline in scikit-learn allows you to chain together multiple steps, such as preprocessing and modeling, into a single object. This makes your code cleaner, helps prevent mistakes, and ensures that all steps are included during model evaluation and hyperparameter tuning. In this lesson, you will learn how to build a pipeline and tune its hyperparameters using GridSearchCV, so you can optimize your entire workflow, not just the model itself.
Building a Pipeline in scikit-learn
A pipeline in scikit-learn is a simple way to bundle preprocessing and modeling steps together. For example, you might want to scale your features before passing them to a Support Vector Machine (SVM) classifier. Instead of doing this in separate steps, you can use a pipeline to connect them.
Here is how you can create a pipeline that first scales the data using StandardScaler and then fits an SVM model:
In this code, the pipeline has two steps. The first step, named scaler, uses StandardScaler to standardize the features. The second step, named svm, uses the SVC model. Naming each step is important because you will use these names when tuning hyperparameters later. With this setup, every time you fit the pipeline, it will first scale the data and then train the SVM, making your workflow both simple and reliable.
Tuning Pipeline Hyperparameters with GridSearchCV
Once you have a pipeline, you can tune its hyperparameters just as you would with a single model. However, when you want to tune parameters inside a pipeline, you need to tell GridSearchCV which step the parameter belongs to. This is done using the double underscore (__) syntax. For example, if you want to tune the C parameter of the SVM step, you would use svm__C.
Here is how you can set up a parameter grid for the pipeline:
This grid tells GridSearchCV to try different values for the C parameter and to test both the linear and rbf kernels for the SVM. The kernel parameter in SVC determines the type of decision boundary the SVM will use. Common options include 'linear' (a straight line or hyperplane), 'rbf' (radial basis function, which allows for more flexible, curved boundaries), and 'poly' (polynomial, which can model even more complex relationships). By tuning svm__kernel, you can control how the SVM separates the classes in your data, which can have a big impact on model performance depending on the underlying patterns in your dataset. The double underscore connects the step name (svm) with the parameter name (C or kernel). You can add more parameters from other steps in the pipeline using the same pattern.
To run GridSearchCV on the pipeline, you use the same approach as before, but now you pass the pipeline as the estimator:
This code will search over all combinations of the specified parameters, using 3-fold cross-validation to evaluate each one. The best combination will be stored in search.best_params_.
