GridSearchCV
machine learning
parameter tuning
Python
data visualization

How to graph grid scores from GridSearchCV?

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

GridSearchCV: A Brief Overview

GridSearchCV is part of the sklearn.model_selection package and is a crucial tool for hyperparameter tuning. It automates the process of exhaustively searching through a specified parameter grid to determine the best model configuration based on cross-validation. The GridSearchCV object will train the model on each combination of parameters and then evaluate it using cross-validation.

Understanding Grid Scores from GridSearchCV

When GridSearchCV completes the search across the parameter grid, it provides a detailed report of grid scores that includes:

  • The parameter combinations that were tested.
  • The mean cross-validated score of the best_estimator.
  • The scores for all parameter combinations.

These grid scores can be accessed through the cv_results_ attribute in the GridSearchCV object. This attribute provides several useful fields such as:

  • mean_test_score: Mean cross-validated score for each parameter combination.
  • std_test_score: Standard deviation of the test score.
  • params: The combination of parameters used for that score.
  • Other diagnostic metrics such as mean_fit_time and mean_score_time.

Extracting the Grid Scores

python
1from sklearn.datasets import load_iris
2from sklearn.model_selection import GridSearchCV
3from sklearn.svm import SVC
4import pandas as pd
5
6# Load dataset
7iris = load_iris()
8X, y = iris.data, iris.target
9
10# Define a simple parameter grid
11param_grid = {'kernel': ('linear', 'rbf'), 'C': [1, 10]}
12
13# Setup the GridSearch
14grid_search = GridSearchCV(SVC(), param_grid, cv=5, return_train_score=True)
15grid_search.fit(X, y)
16
17# Convert results to a DataFrame
18cv_results_df = pd.DataFrame(grid_search.cv_results_)

Graphing Grid Scores

Plotting the grid scores is an excellent way to visualize the performance of different hyperparameter combinations. This can help in identifying trends and selecting hyperparameters. Here we provide a step-by-step guide to plotting the grid scores.

Step 1: Extract Key Metrics

Before plotting, you need to decide which metrics you want to visualize. Common choices include:

  • mean_test_score vs. each hyperparameter.
  • Performance metrics for all parameter combinations.

Step 2: Plot the Grid Scores

Leverage Python's matplotlib or seaborn libraries to plot these scores effectively. Below is a simple example showing how to plot the mean test scores for different combinations of parameters.

python
1import matplotlib.pyplot as plt
2import seaborn as sns
3
4# Create a pivot table for heatmap
5pivot_table = cv_results_df.pivot("param_C", "param_kernel", "mean_test_score")
6
7# Plot heatmap
8plt.figure(figsize=(8, 6))
9sns.heatmap(pivot_table, annot=True, cmap='viridis', cbar_kws={'label': 'Mean Test Score'})
10plt.title('Heatmap of Mean Test Scores for Various Parameter Combinations')
11plt.xlabel('Kernel Type')
12plt.ylabel('C Parameter')
13plt.show()

Step 3: Interpret the Results

The plot visually illustrates which combinations of hyperparameters yield the best performance score on the test data. This is typically represented by the darker areas in a heatmap when using viridis colormap as shown in the example above.

For instance, the visualization can show how a linear kernel with a lower C value could be more favorable if the mean test score is significantly higher for those combinations.

Table Summary: Key Results

Below is a table illustrating key cross-validation results, extracted from the GridSearchCV output, summarizing mean and standard deviation test scores.

Parameter CombinationMean Test ScoreStd Test Score
kernel='linear', C=10.96670.0389
kernel='linear', C=100.96670.0389
kernel='rbf', C=10.96670.0303
kernel='rbf', C=100.96670.0303

Additional Insights

  • Kernel Impact: Observing which kernel might provide consistently higher scores across all C values.
  • C Parameter Sensitivity: How sensitive the model's performance is to changes in the regularization parameter.

Model Performance

While graphs provide visual insights, it’s important to corroborate findings with statistical significance testing where necessary. Ensure that the variations are meaningful in practical terms, not only mathematical.

Trade-offs

Consider the trade-off between model complexity and performance. Models that score the highest in cross-validation might be more complex and prone to overfitting; thus, they should be balanced against simpler alternatives with comparable performance.

By understanding and visualizing grid scores, data scientists can make informed decisions on the hyperparameters that best suit their predictive model objectives.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.