Hyperparameter Tuning — ML Technical Interview Questions & Answers
Grid search, random search and Bayesian optimization.
Hyperparameter Tuning
- Simple meaning: Parameters like weights are learned from data. Hyperparameters you pick: k in KNN, max_depth, learning rate, C. Tuning = search those with CV, not with the test set.
- Picture: Try k = 3,5,7,9 inside cross-validation. Pick the k with the best mean score. Then lock it and score test once.
- Grid search = every combo (slow). Random search = random combos (often enough). sklearn: GridSearchCV, RandomizedSearchCV — use a Pipeline.
- Trap: tuning on test. Trap: 10,000 combos on 80 rows (overfit the CV too).
Technical Interview Tip: Hyperparameters are knobs. Turn them with CV, not the test sheet.
Code Examples for Hyperparameter Tuning
Hyperparameter Tuning — working example
# PCA-style intuition: project 2D → 1D by a direction vector
points = [(1.0, 1.0), (2.0, 2.0), (3.0, 2.8)]
# unit direction along y = x
ux, uy = 0.7071, 0.7071
proj = [x * ux + y * uy for x, y in points]
print([round(v, 3) for v in proj])
# Tiny ensemble: average two model votes
votes = [1, 0, 1]
v2 = [1, 1, 1]
ens = [1 if a + b >= 2 else 0 for a, b in zip(v1, v2)]
print(ens)Hyperparameter Tuning — dim reduction compresses features; ensembles combine models; deep nets/RL/NLP are specialized families — practice the core math idea first.
How to Explain Hyperparameter Tuning to an Interviewer
Parameter vs hyperparameter?
Learned weights vs knobs you set
sklearn tools?
GridSearchCV RandomizedSearchCV