0Pricing
Machine Learning Academy · Lesson

Tuning C and Gamma with a Grid Search

Learners will run a GridSearchCV over C and gamma, interpret the validation heat map, and select the best hyperparameters for an SVM classifier.

Why C and Gamma Must Be Tuned Together

For an RBF SVM, both C and gamma must be tuned simultaneously because their effects interact. A high C with a small gamma may produce a good boundary, but so might a low C with a high gamma. Searching them independently — fixing one while optimising the other — misses this interaction and often settles on a sub-optimal combination. The standard approach is a 2D grid search that evaluates every combination of C and gamma values and picks the pair with the best cross-validated score.

Setting Up the Grid Search

Use GridSearchCV from scikit-learn to evaluate every hyperparameter combination. Wrap your scaler and SVM in a Pipeline so that GridSearchCV refits the scaler on each training fold without leaking test data statistics. Specify the hyperparameter grid using the double-underscore notation svc__C and svc__gamma to target the named step in the pipeline.

from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
from sklearn.preprocessing import StandardScaler
from sklearn.pipeline import Pipeline
from sklearn.datasets import load_breast_cancer
import numpy as np

X, y = load_breast_cancer(return_X_y=True)
pipe = Pipeline([('scaler', StandardScaler()), ('svc', SVC(kernel='rbf'))])
param_grid = {'svc__C': np.logspace(-2, 3, 6), 'svc__gamma': np.logspace(-4, 0, 5)}
grid = GridSearchCV(pipe, param_grid, cv=5, n_jobs=-1)
grid.fit(X, y)
print('Best params:', grid.best_params_)
print('Best CV score:', round(grid.best_score_, 4))

All lessons in this course

  1. Maximum Margin Classifier: Support Vectors and Hyperplane
  2. Soft Margin SVM and the C Parameter
  3. The Kernel Trick: RBF, Polynomial, and Sigmoid Kernels
  4. Tuning C and Gamma with a Grid Search
← Back to Machine Learning Academy