Sub topic 13
Tuning C and gamma with grid search
This chapter assumes you know how C and the RBF gamma work, what cross-validation does, and how to use scikit-learn estimators. Here you will search for good values of both parameters in a way that does not leak test data.
Why C and gamma must be tuned together
C sets how much the model pays for margin violations. A large C gives a narrow margin and few training errors. A small C gives a wide margin and tolerates more errors.
Gamma sets how far the influence of a single training point reaches in the RBF kernel. A large gamma makes each point's influence local, so the boundary becomes wiggly. A small gamma makes the boundary smooth.
The two interact. A large gamma with a large C overfits badly. A small gamma with a small C underfits. Good values often lie along a diagonal band in the (C, gamma) plane, so tuning one parameter while the other stays fixed can miss the best region.
The search procedure
Grid search tries every combination in a grid. Each combination is scored with k-fold cross-validation on the training data. The best combination is refit on all the training data. Only then do you touch the test set, once.
A complete example
from sklearn.datasets import make_moons
from sklearn.model_selection import GridSearchCV, train_test_split
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
X, y = make_moons(n_samples=400, noise=0.25, random_state=0)
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=0)
pipe = make_pipeline(StandardScaler(), SVC(kernel="rbf"))
param_grid = {
"svc__C": [0.1, 1, 10, 100],
"svc__gamma": [0.01, 0.1, 1, 10],
}
grid = GridSearchCV(pipe, param_grid, cv=5)
grid.fit(X_train, y_train)
print(grid.best_params_)
print("test accuracy:", grid.score(X_test, y_test))The scaler sits inside the pipeline. Each fold therefore fits it only on that fold's training part, and the validation part never influences the scaling. The svc__ prefix tells GridSearchCV which pipeline step owns each parameter.
This grid has 16 combinations and 5 folds, so 80 model fits plus one final refit.
Practical advice
- Use a logarithmic grid, such as powers of 10. The effect of C and gamma is multiplicative, not additive.
- If the best value sits on the edge of the grid, extend the grid in that direction.
- Search coarsely first. Then run a finer grid around the best region.
- Compare
grid.best_score_with the test score. A large gap suggests the grid was too fine for the amount of data, or the split was unlucky. - For classification,
cv=5uses stratified folds, which keeps class proportions stable.
Exercise
Exercise
A grid search reports bestparams of C=100 and gamma=10, the largest value of each in the grid. Training accuracy is near 100% but the cross-validation score is much lower. What does this suggest, and what should you do?
Show answer
The best values sit at the edge of the grid, so the optimum may lie beyond it. The gap between training and cross-validation accuracy points to overfitting, which is typical of large C with large gamma. Extend the grid on a log scale in the direction of the edge values and also check smaller values. Then confirm that the best cross-validation score is not at the new edge. If the gap remains, prefer a smaller gamma or C.