Combinatorics interview question

Probability & MathCombinatoricsHyperparameter Tuningmedium

How many times is an SVC model fitted in total when this grid search runs?

python
from sklearn.model_selection import GridSearchCV
from sklearn.svm import SVC

param_grid = [
    {"kernel": ["linear"], "C": [0.1, 1, 10]},
    {"kernel": ["rbf"], "C": [0.1, 1, 10], "gamma": [0.01, 0.1]},
]
search = GridSearchCV(SVC(), param_grid, cv=5)   # refit=True is the default
search.fit(X_train, y_train)
  1. A46 fits: the two dictionaries are separate grids with 3 + 6 = 9 candidates, each fitted on 5 folds, plus one refit of the best one on all training data.
  2. B61 fits: the dictionaries merge into one grid of 2 kernels × 3 values of C × 2 values of gamma = 12 candidates, times 5 folds, plus one final refit.
  3. C45 fits: 3 + 6 = 9 candidates times 5 folds; no extra fit is needed, because the best fold model is reused as the final estimator after the search.
  4. D9 fits: each candidate is fitted once, and cross-validation then re-scores that same fitted model on 5 different validation folds instead of refitting.
Show the answer and explanation

Correct answer: A, 46 fits: the two dictionaries are separate grids with 3 + 6 = 9 candidates, each fitted on 5 folds, plus one refit of the best one on all training data.

Explanation Combinatorics

When param_grid is a list of dictionaries, scikit-learn expands each dictionary into its own grid and searches the union. This avoids meaningless combinations, for example gamma values for a linear kernel that ignores them.

  • Grid 1: 1 kernel × 3 values of C = 3 candidates.
  • Grid 2: 1 kernel × 3 values of C × 2 values of gamma = 6 candidates.
  • Total: 9 candidates, each trained once per fold: 9 × 5 = 45 fits.
  • refit=True (the default) then retrains the best candidate on the full training data, so that best_estimator_ and search.predict work: 46 fits in total.
StageFits
Cross-validation9 × 5 = 45
Final refit1
Total46

Why the other answers fail:

  • A single merged product (2 × 3 × 2 = 12 candidates) is what one dictionary with both kernels would produce; the list form keeps the grids separate.
  • The fold models are not reused as the final estimator; each saw only 80% of the training data, so the refit trains on all of it.
  • Cross-validation does not re-score one model: every fold trains a fresh model on the other four folds and scores it on the held-out fold.

Grid sizes multiply fast: 5 hyperparameters with 4 values each give 4⁵ = 1,024 candidates, or 5,120 fits with 5-fold CV. That is why random search, successive halving or Bayesian optimisation are preferred for larger spaces.

Interview tip: estimate the budget out loud as candidates × folds (+ refit) × time per fit before proposing a grid, and mention nested cross-validation if the tuned score must be an unbiased performance estimate.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Probability & Math questions

  1. An LLM routing service samples one of three candidate reply types from logits with the snippet below, at temperature T = 0.5. Up to simulation noise, what…
  2. A labelling vendor's error rate is either 2% (prior 0.7) or 10% (prior 0.3). You audit two batches of 10 independent labels: the first has 1 error, the second…
  3. A bank fine-tunes a multilingual support assistant on a corpus that is 90% English and 10% Turkish by tokens. Following common multilingual practice, it…
  4. A churn network maps four features x ∈ ℝ⁴ through a = W₁x + b₁ (W₁ is 3 × 4), h = ReLU(a) and z = W₂h + b₂ (W₂ is 2 × 3). Writing the Jacobian with entry (i,…
  5. Each week a loyalty-app user is either Engaged or Idle. An Engaged user becomes Idle the next week with probability 0.10, and an Idle user becomes Engaged…