Sling Academy
Home/Scikit-Learn/Hyperparameter Tuning with `GridSearchCV` in Scikit-Learn

Hyperparameter Tuning with `GridSearchCV` in Scikit-Learn

Last updated: December 17, 2024

When working with machine learning models, one often encounters the need to fine-tune certain parameters to optimize their performance. This process is known as hyperparameter tuning, and it is crucial for model success. A powerful tool for this task is GridSearchCV from the Scikit-Learn library.

Understanding Hyperparameters

Before diving into GridSearchCV, let's clarify what hyperparameters are. Unlike parameters that are learned during the model training process (like weights and biases in neural networks), hyperparameters are set before the learning process begins. They can include metrics such as the learning rate, number of trees in a random forest, or the number of nearest neighbors in a KNN model.

What is GridSearchCV?

GridSearchCV is a technique in Scikit-Learn that performs an exhaustive search over a specified set of hyperparameters for an estimator. It automates the process of testing various hyperparameter combinations to determine the one that yields the best performance.

The 'CV' in GridSearchCV stands for cross-validation. This means that for each set of hyperparameters, the model is trained and evaluated using cross-validation, ensuring robust performance estimation.

Setting Up GridSearchCV

To use GridSearchCV, you begin by importing the necessary libraries and preparing your data. Here's a simple example with a Support Vector Machine (SVM) classifier:

from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load sample dataset
iris = load_iris()
X, y = iris.data, iris.target

# Split data into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

In this example, we are using the Iris dataset and splitting it into training and test sets.

Defining the Hyperparameter Grid

Next, define a dictionary containing possible values for each hyperparameter you wish to tune:

# Define a hyperparameter grid
param_grid = {
    'C': [0.1, 1, 10, 100],
    'kernel': ['linear', 'rbf', 'poly']
}

In this grid, we are tuning the penalty parameter C and exploring different kernel types for the SVM classifier.

Conducting the Search with GridSearchCV

Now, pass this grid to GridSearchCV along with the model and fit it to the training data:

# Create a base model
svc = SVC()

# Initialize grid search
grid_search = GridSearchCV(estimator=svc, param_grid=param_grid, cv=5, verbose=1)

# Fit the grid search to the data
grid_search.fit(X_train, y_train)

This code initializes GridSearchCV with 5-fold cross-validation. The verbose=1 allows us to see the progress of the fitting process.

Evaluating Results

After the grid search is complete, you can examine the results and determine the best parameters:

# Get the best parameters and estimator
best_params = grid_search.best_params_
best_estimator = grid_search.best_estimator_

print(f"Best Parameters: {best_params}")

This will output the best parameter combination found during the search. You can use best_estimator to mention this model in your evaluative processes or for final predictions.

Additionally, you can evaluate the performance of the best model on the test set:

# Evaluate the model
accuracy = best_estimator.score(X_test, y_test)
print(f"Accuracy on the test set: {accuracy:.2f}")

This will give you the accuracy of your tuned model, helping you understand how well it generalizes to unseen data.

Conclusion

In summary, GridSearchCV is an extremely useful tool in machine learning for fine-tuning hyperparameters systematically. By testing combinations of parameters in a grid format, it allows practitioners to make informed decisions on how to configure their models for optimal performance. While grid search can be computationally expensive for large parameter spaces, its benefits greatly outweigh the costs when ensuring model efficacy.

Next Article: Understanding `RandomizedSearchCV` in Scikit-Learn

Previous Article: Using Scikit-Learn's `train_test_split` for Model Validation

Series: Scikit-Learn Tutorials

Scikit-Learn

You May Also Like

  • Generating Gaussian Quantiles with Scikit-Learn
  • Spectral Biclustering with Scikit-Learn
  • Scikit-Learn Complete Cheat Sheet
  • ValueError: Estimator Does Not Support Sparse Input in Scikit-Learn
  • Scikit-Learn TypeError: Cannot Broadcast Due to Shape Mismatch
  • AttributeError: 'dict' Object Has No Attribute 'predict' in Scikit-Learn
  • KeyError: Missing 'param_grid' in Scikit-Learn GridSearchCV
  • Scikit-Learn ValueError: 'max_iter' Must Be Positive Integer
  • Fixing Log Function Error with Negative Values in Scikit-Learn
  • RuntimeError: Distributed Computing Backend Not Found in Scikit-Learn
  • Scikit-Learn TypeError: '<' Not Supported Between 'str' and 'int'
  • AttributeError: GridSearchCV Has No Attribute 'fit_transform' in Scikit-Learn
  • Fixing Scikit-Learn Split Error: Number of Splits > Number of Samples
  • Scikit-Learn TypeError: Cannot Concatenate 'str' and 'int'
  • ValueError: Cannot Use 'predict' Before Fitting Model in Scikit-Learn
  • Fixing AttributeError: NoneType Has No Attribute 'predict' in Scikit-Learn
  • Scikit-Learn ValueError: Cannot Reshape Array of Incorrect Size
  • LinAlgError: Matrix is Singular to Machine Precision in Scikit-Learn
  • Fixing TypeError: ndarray Object is Not Callable in Scikit-Learn