GridSearchCV tool introduction

Table of contents

1. Definition

2. Workflow

3. Sample code

4. Summary

1. Definition

GridSearchCV is a tool for hyperparameter tuning that performs cross-validation within a given parameter grid to determine the best parameter combination. Find the best parameters through an exhaustive search, i.e. try all possible parameter combinations and use cross-validation to evaluate the performance of each parameter combination.

2. Workflow

1) Define the model to be tuned, including model type and initial parameters.
2) Define the parameter grid to be searched, that is, a dictionary or list of possible values ​​for each parameter.
3) Create a GridSearchCV object and pass in the model, parameter grid, and evaluation indicators as parameters.
5) Call the fit method, GridSearchCV will perform cross-validation, traverse all combinations in the parameter grid, and evaluate model performance on each combination.
6) Based on the results of cross-validation, return the parameter combination with the best performance.

3. Sample code

from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC

# 定义要调优的模型
model = SVC()

# 定义参数网格
param_grid = {
    'C': [0.1, 1, 10],
    'kernel': ['linear', 'rbf'],
    'gamma': [0.1, 1, 10]
}

# 创建GridSearchCV对象
grid_search = GridSearchCV(model, param_grid, cv=5)

# 执行超参数搜索
grid_search.fit(X, y)

# 输出最佳参数组合和最佳得分
print("Best parameters:", grid_search.best_params_)
print("Best score:", grid_search.best_score_)


In the above example, we use GridSearchCV to tune a support vector machine model (SVC). We define a parameter grid, which contains different values ​​of three parameters (C, kernel and gamma). The cv parameter specifies the fold number of cross-validation. By calling the fit method, GridSearchCV performs cross-validation and parameter search. After the search is completed, the best parameter combination can be obtained through the best_params_ attribute, and the best score can be obtained through the best_score_ attribute.

4. Summary

GridSearchCV automates the search for optimal parameter combinations, reducing manual tuning efforts and providing a reliable assessment of model performance. However, it can become computationally intensive when the parameter space is large due to the need to try all possible parameter combinations. Therefore, you need to pay attention to the size of the parameter grid and the limitations of computing resources when using GridSearchCV.

Guess you like

Origin blog.csdn.net/m0_37738114/article/details/133565493