GridSearchCV with Scikit-Learn and Python


In the world of machine learning, finding the optimal set of hyperparameters for a model can significantly impact its performance and accuracy. However, searching through all possible combinations manually can be an incredibly time-consuming and error-prone process. This is where GridSearchCV, a powerful tool provided by Scikit-Learn library in Python, comes to the rescue.

GridSearchCV automates the process of hyperparameter tuning by exhaustively searching through a predefined grid of parameter combinations, evaluating each combination using cross-validation, and providing us with the best set of parameters that maximize the model’s performance. It proves to be a game-changer for developers who want to fine-tune their models effectively without spending excessive time on trial and error.

This blog post will delve into the core concepts of GridSearchCV and demonstrate how it can be leveraged with Scikit-Learn’s machine learning algorithms in Python. We will explore its functionality, understand its implementation details, and demonstrate how it optimizes hyperparameters to enhance model performance.

Grid Search for HyperParameters

Grid search is a technique used to find the best combination of hyperparameters for a machine learning algorithm. It works by exhaustively searching through a predefined set of hyperparameter values and evaluating each combination using a scoring metric. The goal is to identify the combination that gives the highest score and therefore improves the model’s performance.

The process of grid search begins by specifying the hyperparameters we want to tune and their respective range of values. These ranges can be defined as discrete values or continuous ranges depending on the hyperparameter type. The grid search algorithm then sets up an experiment with all possible combinations of these hyperparameters.

To evaluate each combination, we split our data into training and validation sets. We use the training set to train a model with a specific combination of hyperparameters, and then evaluate its performance on the validation set using a chosen scoring metric (e.g., accuracy or F1-score). This step is repeated for every combination in our grid.

After evaluating all combinations, we select the one with the highest score as our optimal set of hyperparameters. These optimal values can then be used to train a final model on all available training data.

One important thing to keep in mind is that grid search can become computationally expensive when dealing with large datasets or complex models, as it requires training and evaluating multiple models for every combination of hyperparameters.

Understanding GridSearchCV Class

Let’s take a look at the class signature and arguments for Scikit-Learn’s GridSearchCV Tool:

class sklearn.model_selection.GridSearchCV(estimator, param_grid, *, scoring=None, n_jobs=None, refit=True, cv=None, verbose=0, pre_dispatch='2*n_jobs', error_score=nan, return_train_score=False)[source]

Here is an explanation of each argument:

  1. estimator: This is the estimator object that implements the scikit-learn estimator interface. It is the model or algorithm that you want to optimize using grid search.
  2. param_grid: A dictionary or a list of dictionaries with parameters names as keys and lists of parameter settings to try as values. This specifies the grid of hyperparameters that will be searched over.
  3. scoring: A string, callable, list, tuple or dictionary that defines how the performance of the model will be evaluated during cross-validation. If a single score is used, it can be a string representing a scoring metric or a callable that returns a single value. If multiple scores are used, it can be a list/tuple of strings representing different scoring metrics, a callable returning a dictionary of metric scores, or a dictionary with metric names as keys and callables as values.
  4. n_jobs: An integer specifying the number of parallel jobs to run during grid search. By default, it is set to None (1 job), but it can also be set to -1 (use all available processors).
  5. refit: A boolean, string or callable indicating whether to refit an estimator using the best-found parameters on the whole dataset after grid search is complete. If set to True, the estimator will be refitted with the best parameters and made available at the best_estimator_ attribute.
  6. cv: The cross-validation strategy to use during grid search. It can be an integer specifying the number of folds in a (Stratified) KFold, a cross-validation splitter object, or an iterable yielding (train, test) splits as arrays of indices.
  7. verbose: An integer controlling the verbosity level during grid search. Higher values enable more detailed messages about computation time, scores, and parameter indexes.
  8. pre_dispatch: An integer or string controlling the number of jobs that get dispatched during parallel execution. Reducing this number can be useful to avoid memory consumption issues. It can be set to an exact number of total jobs, a formula expression as a function of n_jobs, or None (immediate creation and spawning of all jobs).
  9. error_score: A string or numeric value specifying how to handle errors that occur during estimator fitting. If set to ‘raise’, the error is raised. If a numeric value is given, a warning is raised but the grid search will proceed.
  10. return_train_score: A boolean indicating whether to include training scores in the cv_results_ attribute. Computing training scores can provide insights on overfitting/underfitting trade-off, but it can be computationally expensive and is not necessary for parameter selection.

Example of GridSearchCV in Python

Let’s walk through an example of using GridSearchCV on the built-in Iris dataset in Scikit-Learn, to find the best parameters for a Support Vector Machine (SVM) classifier.

First, let’s import the necessary libraries:

import numpy as np
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.svm import SVC

Next, let’s load the Iris dataset and split it into training and testing sets:

iris = load_iris()
X, y =,

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

Now, we define the parameter grid to search over. In this example, we’ll consider two hyperparameters for the SVM: C (regularization parameter) and kernel (type of SVM kernel). We’ll create a dictionary where each key is the name of a hyperparameter and its value is a list of parameter values to try:

param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'poly', 'rbf']}

We instantiate an SVM classifier object:

svm = SVC()

Now we can create an instance of GridSearchCV. Here we need to provide the estimator (the SVM classifier), the parameter grid, and specify the scoring metric to evaluate the performance of different parameter combinations. We’ll use accuracy as our scoring metric:

grid_search = GridSearchCV(svm, param_grid, scoring='accuracy')

Next, we fit GridSearchCV with our training data to search for the best parameters:, y_train)

Once the grid search is complete, we can access the best parameters found:

best_params = grid_search.best_params_
print("Best parameters:", best_params)

We can also access the best estimator (classifier trained on the entire training set with the best parameters):

best_estimator = grid_search.best_estimator_
print("Best estimator:", best_estimator)

To evaluate the performance of the best estimator on unseen data, we can use the testing set:

accuracy = best_estimator.score(X_test, y_test)
print("Accuracy on test set:", accuracy)

Finally, we can also access other attributes of GridSearchCV, like the cross-validated scores for each parameter combination:

cv_results = grid_search.cv_results_
mean_scores = cv_results['mean_test_score']

for params, mean_score in zip(cv_results['params'], mean_scores):
    print(f"Parameters: {params}, Mean Score: {mean_score}")

Visualizing GridSearchCV Results

You can visualize the results of a grid search using matplotlib. One common approach is to create a heatmap that shows the performance (e.g., accuracy) of different parameter combinations.

Here’s an example of how to visualize the grid search results using a heatmap:

import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split, GridSearchCV
from sklearn.svm import SVC

# Load the Iris dataset and split into training and testing sets
iris = load_iris()
X, y =,

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Define the parameter grid to search over
param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'poly', 'rbf']}

# Instantiate an SVM classifier object
svm = SVC()

# Create an instance of GridSearchCV
grid_search = GridSearchCV(svm, param_grid, scoring='accuracy')

# Fit GridSearchCV with the training data to search for the best parameters, y_train)

# Access the best parameters found
best_params = grid_search.best_params_
print("Best parameters:", best_params)

# Access the best estimator (classifier trained on the entire training set with the best parameters)
best_estimator = grid_search.best_estimator_
print("Best estimator:", best_estimator)

# Evaluate the performance of the best estimator on unseen data using the testing set
accuracy = best_estimator.score(X_test, y_test)
print("Accuracy on test set:", accuracy)

# Visualize grid search results using a heatmap
mean_scores = grid_search.cv_results_['mean_test_score']
mean_scores = np.array(mean_scores).reshape(len(param_grid['C']), len(param_grid['kernel']))

plt.figure(figsize=(8, 6))
plt.imshow(mean_scores, interpolation='nearest', cmap='viridis')
plt.title('Grid Search Mean Test Scores', fontsize=16)
plt.xticks(np.arange(len(param_grid['kernel'])), param_grid['kernel'])
plt.yticks(np.arange(len(param_grid['C'])), param_grid['C'])
plt.colorbar(label='Mean Test Score')

In this example, we used mean_test_score from grid_search.cv_results_, which contains the mean cross-validated scores for each parameter combination. We reshaped it into a 2D array that corresponds to the shape of our parameter grid.

We then created a heatmap using imshow() function from matplotlib and customized it with title, axes labels, tick labels, and colorbar. The resulting heatmap will have rows corresponding to different C values, columns corresponding to different kernel values, and the color intensity representing the mean test score obtained for each combination.


GridSearchCV is a powerful tool for hyperparameter tuning in machine learning models. By exhaustively searching through a specified parameter grid, it helps optimize model performance and ensure that the best combination of hyperparameters is selected.

One of the key advantages of GridSearchCV is its ability to automate the process of hyperparameter tuning, saving time and effort for data scientists and researchers. It eliminates the need for manual parameter tuning, allowing for a more efficient and consistent approach to model optimization.

Here are some tips and tricks to make the most out of GridSearchCV:

  1. Start with a coarse grid: Begin by defining a broad range of potential values for each hyperparameter. This allows you to quickly explore the parameter space and understand the impact of different values on model performance.
  2. Narrow down the grid: Once you have identified promising regions in the parameter space, refine your search by selecting narrower intervals or specific values to further improve the model’s performance.
  3. Use multiple scoring metrics: GridSearchCV allows you to specify multiple scoring metrics to evaluate the models. Take advantage of this functionality to consider various performance aspects (e.g., accuracy, precision, recall) and select parameters that optimize across multiple criteria.
  4. Speed up computation with parallel processing: If you have access to multiple processors or cores, enable parallel processing in GridSearchCV using the ‘n_jobs’ parameter. This can significantly speed up the search process, especially when dealing with large datasets or complex models.
  5. Consider nested cross-validation: If you have limited data available, consider implementing nested cross-validation with GridSearchCV. This technique helps prevent overfitting and provides a more robust estimation of model performance.
  6. Explore other search techniques: GridSearchCV employs an exhaustive approach, which can be computationally expensive for large parameter grids. If computational resources are a concern, try using randomized search or Bayesian optimization techniques implemented in packages like scikit-optimize or Optuna.

In conclusion, GridSearchCV is a valuable tool for finding the optimal combination of hyperparameters in machine learning models. By systematically exploring the parameter space, it helps to enhance model performance and generalization. Following these tips and tricks will assist you in efficiently utilizing GridSearchCV and optimizing your machine learning models for superior results.

Pierian Training
Pierian Training
Pierian Training is a leading provider of high-quality technology training, with a focus on data science and cloud computing. Pierian Training offers live instructor-led training, self-paced online video courses, and private group and cohort training programs to support enterprises looking to upskill their employees.

You May Also Like

Data Science, Tutorials

Guide to NLTK – Natural Language Toolkit for Python

Introduction Natural Language Processing (NLP) lies at the heart of countless applications we use every day, from voice assistants to spam filters and machine translation. It allows machines to understand, interpret, and generate human language, bridging the gap between humans and computers. Within the vast landscape of NLP tools and techniques, the Natural Language Toolkit […]

Python Basics, Tutorials

Plotting Time Series in Python: A Complete Guide

Introduction Time series data is a type of data that is collected over time at regular intervals. It can be used to analyze trends, patterns, and behaviors over time. In order to effectively analyze time series data, it is important to visualize it in a way that is easy to understand. This is where plotting […]

Python Basics, Tutorials

A Beginner’s Guide to Scipy.ndimage

Introduction Scipy.ndimage is a package in the Scipy library that is used to perform image processing tasks. It provides functions to perform operations like filtering, interpolation, and morphological operations on images. In this guide, we will cover the basics of Scipy.ndimage and how to use it to manipulate images. What is Scipy.ndimage? Scipy.ndimage is a […]