SVC Grid Search Over Log-Spaced C and Gamma

Quick Overview

Write a log-spaced grid helper and a grid search that tunes C and gamma for an RBF support vector classifier inside a scaling pipeline. It tests leak-free cross-validation, stratified splitting, reproducible seeding and why SVM hyperparameters are searched on a log scale.

SVC Grid Search Over Log-Spaced C and Gamma

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: easy

Interview Round: Other

Implement `make_log_grid(low_exp, high_exp, num)` returning `num` values evenly spaced in log10 from `10**low_exp` to `10**high_exp` (inclusive). Then implement `run_svc_grid_search(X, y, c_values, gamma_values, cv=5, test_size=0.25, seed=0)`, which tunes an RBF `SVC` inside a scaling pipeline and reports CV and held-out results. - Split with `train_test_split(test_size=test_size, random_state=seed, stratify=y)`. - Pipeline steps named exactly `"scaler"` (`StandardScaler`) then `"svc"` (`SVC(kernel="rbf")`). Grid keys: `svc__C`, `svc__gamma`. - `GridSearchCV` with `scoring="accuracy"`, `refit=True`, and `StratifiedKFold(n_splits=cv, shuffle=True, random_state=seed)`, fit on the train split only. - Return a dict: `best_params` (dict), `best_cv_score` (float), `test_score` (float accuracy of the refitted model on the test split), `search` (the fitted `GridSearchCV`). Example: ```python make_log_grid(-2, 2, 5) # values: 0.01, 0.1, 1.0, 10.0, 100.0 ``` ```hint Spacing in the exponent Values evenly spaced in log10 have evenly spaced exponents. Look for a NumPy function whose endpoints are given as exponents. ``` ### What a Strong Answer Covers - A log grid that includes both endpoints and is built from evenly spaced exponents. - A stratified, seeded split made before any fitting, with the search fit on the training rows only. - Scaling inside the pipeline so each CV fold fits its own scaler, and grid keys that follow the `step__param` naming. - `GridSearchCV` with the seeded, shuffled `StratifiedKFold`, accuracy scoring and refit, returning the four keys with Python `float` scores. - Why `C` and `gamma` are searched on a log scale. ### Follow-up Questions - The best pair sits on the edge of the grid, for example the largest `C` with the smallest `gamma`. What do you do next? - How does the number of fits grow with the grid and the number of folds, and when would you switch to a randomized or successive halving search? - Why is `best_cv_score` usually a little higher than `test_score`, and what does a large gap tell you?

Overview: Write a log-spaced grid helper and a grid search that tunes C and gamma for an RBF support vector classifier inside a scaling pipeline. It tests leak-free cross-validation, stratified splitting, reproducible seeding and why SVM hyperparameters are searched on a log scale.

General logo
General
Nov 24, 2026
easyMachine Learning EngineerOtherMachine Learning
0
0

Implement make_log_grid(low_exp, high_exp, num) returning num values evenly spaced in log10 from 10**low_exp to 10**high_exp (inclusive). Then implement run_svc_grid_search(X, y, c_values, gamma_values, cv=5, test_size=0.25, seed=0), which tunes an RBF SVC inside a scaling pipeline and reports CV and held-out results.

  • Split with train_test_split(test_size=test_size, random_state=seed, stratify=y) .
  • Pipeline steps named exactly "scaler" ( StandardScaler ) then "svc" ( SVC(kernel="rbf") ). Grid keys: svc__C , svc__gamma .
  • GridSearchCV with scoring="accuracy" , refit=True , and StratifiedKFold(n_splits=cv, shuffle=True, random_state=seed) , fit on the train split only.
  • Return a dict: best_params (dict), best_cv_score (float), test_score (float accuracy of the refitted model on the test split), search (the fitted GridSearchCV ).

Example:

make_log_grid(-2, 2, 5)
# values: 0.01, 0.1, 1.0, 10.0, 100.0

What a Strong Answer Covers Guidance

  • A log grid that includes both endpoints and is built from evenly spaced exponents.
  • A stratified, seeded split made before any fitting, with the search fit on the training rows only.
  • Scaling inside the pipeline so each CV fold fits its own scaler, and grid keys that follow the step__param naming.
  • GridSearchCV with the seeded, shuffled StratifiedKFold , accuracy scoring and refit, returning the four keys with Python float scores.
  • Why C and gamma are searched on a log scale.

Follow-up Questions Guidance

  • The best pair sits on the edge of the grid, for example the largest C with the smallest gamma . What do you do next?
  • How does the number of fits grow with the grid and the number of folds, and when would you switch to a randomized or successive halving search?
  • Why is best_cv_score usually a little higher than test_score , and what does a large gap tell you?