python
  1. python-grid-search

Python Grid Search

Syntax

GridSearchCV(estimator, param_grid, scoring=None, n_jobs=None, iid=False, refit=True, cv='warn', verbose=0, pre_dispatch='2*n_jobs', error_score='raise-deprecating')

Example

from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC
from sklearn.datasets import load_iris
iris = load_iris()
X = iris.data
y = iris.target
param_grid = {'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf']}
svc = SVC()
clf = GridSearchCV(svc, param_grid)
clf.fit(X, y)

Output

GridSearchCV(cv=None, error_score='raise-deprecating',
             estimator=SVC(C=1.0, cache_size=200, class_weight=None,
                           coef0=0.0, decision_function_shape='ovr', degree=3,
                           gamma='auto_deprecated', kernel='rbf', max_iter=-1,
                           probability=False, random_state=None, shrinking=True,
                           tol=0.001, verbose=False),
             iid=False, n_jobs=None,
             param_grid={'C': [0.1, 1, 10], 'kernel': ['linear', 'rbf']},
             pre_dispatch='2*n_jobs', refit=True, return_train_score=False,
             scoring=None, verbose=0)

Explanation

GridSearchCV is a function in scikit-learn used to search for the optimal set of hyperparameters for a given estimator. We provide the estimator that we want to optimize, along with the parameter grid we want to search through and GridSearchCV tests each unique combination of hyperparameters in the grid to find the combination that results in the best validation score. It is important to note that GridSearchCV does not perform the actual training of the model, but rather ensures that the best hyperparameters are used when training the model.

Use

GridSearchCV is commonly used in machine learning when optimizing a model's performance. The optimal set of hyperparameters can improve the accuracy and performance of the model on the validation set or even in real-world usage.

Important Points

  • GridSearchCV is used for finding the optimal set of hyperparameters for a given estimator.
  • It searches the parameter grid to find the combination that results in the best validation score.
  • GridSearchCV does not perform the actual training of the model but ensures that the best hyperparameters are used when training the model.

Summary

GridSearchCV is a powerful tool to optimize hyperparameters for a given estimator. It helps improve the performance and accuracy of the model without the need for manual tuning of the hyperparameters.

Published on: