Fatskills
Practice. Master. Repeat.
Study Guide: Data Science and Machine Learning 101: Machine Learning Core Hyperparameter Tuning Grid Search Random Search Bayesian Optimization
Source: https://www.fatskills.com/data-science/chapter/data-science-and-machine-learning-data-science-and-machine-learning-machine-learning-core-hyperparameter-tuning-grid-search-random-search-bayesian-optimization

Data Science and Machine Learning 101: Machine Learning Core Hyperparameter Tuning Grid Search Random Search Bayesian Optimization

By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.

⏱️ ~5 min read

What This Is

Hyperparameter tuning is the systematic search for the best settings that control a learning algorithm (e.g., tree depth, learning rate, regularization strength). These knobs are not learned from the data; they must be chosen before training. Picking good values can shrink error dramatically, turning a mediocre model into a production‑ready one.
Real‑world example: A telecom company builds a churn‑prediction model with XGBoost. The default max‑depth = 6 and learning‑rate = 0.3 give 78 % AUC, but after a focused search the optimal depth = 4 and η = 0.07 push AUC to 84 %—enough to justify a targeted retention campaign.


Key Terms & Formulas

  • Hyperparameter – A configuration parameter set outside the training loop (e.g., n_estimators, C, kernel).
  • Grid Search – Exhaustive Cartesian product of a predefined hyperparameter grid. Use when the search space is small and you need certainty.
  • Random Search – Samples hyperparameter combos uniformly (or log‑uniformly) from a space. Often finds near‑optimal settings faster than grid when dimensions > 3.
  • Bayesian Optimization – Models the validation loss as a probabilistic surrogate (usually Gaussian Process) and selects the next point by maximizing an acquisition function (e.g., Expected Improvement). Best for expensive models.
  • Cross‑Validation (CV) – Splits data into k folds; each hyperparameter set is evaluated on every fold. Metric averaged over folds = CV score.
  • Validation Curve – Plots a single hyperparameter vs. CV score to visualize under‑/over‑fitting.
  • Early Stopping – Stops training when validation loss stops improving for p rounds; effectively a hyperparameter (patience).
  • Search Space – The domain of each hyperparameter (e.g., logspace(-4, 0, 10) for alpha).
  • Acquisition Function – In Bayesian opt., a utility function like EI = E[max(0, f_best – f(x))] that balances exploration vs. exploitation.
  • Scoring Metric – The objective you optimize (e.g., roc_auc, neg_log_loss). Must align with business goal.
  • Nested CV – Outer CV for unbiased performance estimate, inner CV for hyperparameter selection; prevents “leakage” of test information.


Step‑by‑Step / Process Flow

  1. Load & splitX_train, X_test, y_train, y_test = train_test_split(...). Keep a hold‑out set untouched for final evaluation.
  2. Define baseline – Fit a quick model with default hyperparameters; record baseline CV score.
  3. Specify search space – Create a dict of hyperparameter ranges (grid, distributions, or bounds for Bayesian).
    python
    param_grid = {
    "max_depth": [3, 5, 7],
    "learning_rate": np.logspace(-3, -1, 5),
    "n_estimators": [100, 300, 500]
    }
  4. Choose optimizer
  5. Small grid → GridSearchCV.
  6. Large/continuous → RandomizedSearchCV (n_iter=50).
  7. Expensive model → skopt.BayesSearchCV or optuna.create_study().
  8. Run CV – Fit the optimizer on X_train, y_train. It will internally perform k-fold CV for each candidate and keep the best according to scoring.
  9. Validate & finalize – Retrain the model on the full training set with the best hyperparameters, evaluate on the untouched test set, and log the final metric.

Common Mistakes

Mistake Correction
Using the test set for hyperparameter selection – leaks information and inflates performance. Keep a strict hold‑out; perform all tuning on training data (via CV). Use the test set only once at the very end.
Searching a single hyperparameter at a time – ignores interactions, often yields sub‑optimal combos. Tune multiple hyperparameters jointly (grid or random) or use Bayesian methods that capture dependencies.
Choosing a narrow search range – e.g., C only between 0.1‑1 when optimal is 10. Start with a wide, log‑scaled range (e.g., logspace(-4, 4)) and narrow down after a coarse search.
Ignoring computational budget – running thousands of models on the full dataset. Use subset sampling or early stopping; parallelize (n_jobs=-1) and set a reasonable max_iter for random/Bayesian searches.
Evaluating with the wrong metric – optimizing accuracy on a heavily imbalanced churn dataset. Align the scoring metric with the business goal (e.g., roc_auc or average_precision for imbalance).


Data Science Interview / Practical Insights

  1. “When would you prefer Random Search over Grid Search?” – Expect an answer that mentions high‑dimensional spaces, limited budget, and the fact that Random Search explores more unique configurations.
  2. “Explain the role of the acquisition function in Bayesian Optimization.” – Mention that it decides where to sample next by balancing exploitation (low predicted loss) and exploration (high uncertainty).
  3. “How does nested cross‑validation prevent over‑optimistic estimates?” – Highlight the two‑level CV: inner loop selects hyperparameters, outer loop evaluates the whole pipeline on unseen folds.
  4. “What are the trade‑offs between early stopping and a fixed number of estimators?” – Discuss that early stopping adapts to the data, can reduce over‑fitting, but adds a validation split; fixed estimators are simpler but may waste resources.

Quick Check Questions

  1. Scenario: Your XGBoost model’s validation loss plateaus after 150 trees, but you set n_estimators=500.
    Answer: Use early stopping (early_stopping_rounds=30) to stop training automatically.
    Why: Saves time and prevents over‑fitting by halting when no improvement is seen.

  2. Scenario: You have three hyperparameters to tune, each with 5 possible values. Grid Search would evaluate 125 models. You only have time for ~30 fits.
    Answer: Choose Random Search with n_iter=30.
    Why: Random sampling covers the space more efficiently than a truncated grid.

  3. Scenario: After tuning, the test AUC is significantly lower than the CV AUC.
    Answer: You likely performed data leakage (e.g., used test data in CV).
    Why: Leakage inflates CV scores; the true performance appears only on the untouched test set.


Last‑Minute Cram Sheet (10 one‑liners)

  1. Grid Search = exhaustive Cartesian product; best for ≤ 3 hyperparameters.
  2. Random Search samples uniformly (or log‑uniformly); often finds a good solution 10× faster than grid.
  3. Bayesian Opt. builds a surrogate (GP) → selects next point via Acquisition Function (EI, UCB, PI).
  4. CV score = mean metric over k folds; use scoring='neg_log_loss' for probabilistic models.
  5. Early Stoppingmodel.fit(..., eval_set=[(X_val, y_val)], early_stopping_rounds=20).
  6. Nested CV → outer CV for unbiased estimate, inner CV for hyperparameter search.
  7. Log‑scale ranges (np.logspace) are essential for regularization (C, alpha) and learning rates.
  8. Parallelism – set n_jobs=-1 in scikit‑learn search objects to use all cores.
  9. ⚠️ Don’t tune on the test set; it must stay completely unseen until final reporting.
  10. Acquisition Function (EI) = E[max(0, f_best - f(x))]; higher EI → more promising candidate.


ADVERTISEMENT