liveBook · Manning

Grid search

As explained in Grokking Machine Learning

A model-selection technique that evaluates an exhaustive set of specified hyperparameter combinations.

Grid search trains a model with different combinations of specified hyperparameters, then selects the combination that performs best on a validation set. section 13.5

For an SVM with an RBF kernel, a small search can proceed like this:

  • Choose candidate values for gamma and C; pairing two values for each gives four models to compare. section 13.5
  • Train each model, compare its validation performance using a chosen metric, and keep the best-performing combination. section 13.5

Comparing SVM settings

In one comparison, gamma = 0.1 and C = 10 achieved the highest validation accuracy among the tested combinations: 0.72. section 13.5

svm_parameters = {'kernel': ['rbf'],
                  'C': [0.01, 0.1, 1 , 10, 100],
                  'gamma': [0.01, 0.1, 1, 10, 100]
                 }                       #1

svm = SVC()                              #2

svm_gs = GridSearchCV(estimator = svm,
param_grid = svm_parameters)             #3

svm_gs.fit(features_train, labels_train) #4

This Scikit-learn example defines candidate values for C and gamma, creates a GridSearchCV object, and fits it to the training data. section 13.5

How Grokking Machine Learning covers it

How deep it goes
Defines the search and demonstrates candidate combinations, validation scoring, and Scikit-learn implementation.
Examples
Uses an SVM with an RBF kernel, varying gamma and C and comparing validation accuracy.
What it assumes
Assumes familiarity with model hyperparameters, SVM settings such as gamma and C, and training and validation data.
What you can do after
Apply it via Scikit-learn's GridSearchCV to search parameter combinations and retrieve the best estimator.
How it connects
Connects hyperparameter tuning to SVMs, validation sets, and performance metrics such as accuracy and F1-score.

Also explained in

Compare how 6 books explain grid search →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Grokking Machine Learning. The section links point to the parts of the book it draws on.