Every classifier in this unit so far was trained with hyperparameters chosen by hand, not discovered from the data. GridSearchCV automates that search: it trains and cross-validates a model once for every combination of hyperparameters you specify, and reports the combination that scored best.
svm_grid_search.py
from sklearn.svm import SVC
from sklearn.model_selection import GridSearchCV
hyperparam_grid = {
"C": [0.1, 1, 3, 9, 27],
"gamma": [0.001, 0.01, 0.1, 1, 10],
}
search = GridSearchCV(SVC(kernel="rbf"), hyperparam_grid, cv=4)
search.fit(X_train, y_train)
print(f"Best C: {search.best_params_['C']}")
print(f"Best gamma: {search.best_params_['gamma']}")
print(f"Best cross-validated accuracy: {search.best_score_:.3f}")
Sample output
Best C: 1
Best gamma: 0.1
Best cross-validated accuracy: 0.880
Cross-Validation: Why One Split Is Not Enough
A single train/test split gives one accuracy number, which depends partly on which rows happened to land in the test set. $k$-fold cross-validation trains and evaluates the model $k$ times, each time holding out a different fold as the test set, and reports the mean and standard deviation across all $k$ runs, a far more honest picture of how the model actually performs.
cross_validation.py
from sklearn.model_selection import cross_val_score
scores = cross_val_score(forest, X_train, y_train, cv=5)
print(f"CV accuracy: {scores.mean():.3f} (+/- {scores.std():.3f})")
Caution: A Single Split Can Flatter or Punish a Model by Luck
A model evaluated on only one train/test split can look stronger or weaker than it really is, purely because of which examples landed in the test fold. A standard deviation above roughly 0.03 to 0.05 across folds is a sign the dataset is small relative to its difficulty, or that the model is unstable, worth investigating before trusting the mean score at all.

