← capítulo

Tuning hyperparameters

Some numbers a model learns from data. Some you have to hand it first — depth, neighbors, C, gamma. Those are hyperparameters.

Tuning is the honest loop around cross-validation: try a configuration, score it with k-fold, keep the winner — without fooling yourself.

Grid folklore, then Bergstra

The surface we're searching

Two knobs, C and gamma, five values each. Every cell is a 5-fold CV accuracy. The search discovers this one cell at a time — it can't see it first.

Bright ridge in the middle. Brightest cell: C=10, gamma=0.01.

The objective, in one line

Cross-validated score of a configuration λ\lambda:

CV(λ)=1kj=1k1FjiFj1 ⁣[f^λ(j)(xi)=yi]\mathrm{CV}(\lambda) = \frac{1}{k} \sum_{j=1}^{k} \frac{1}{|F_j|} \sum_{i \in F_j} \mathbb{1}\!\left[\, \hat{f}_{\lambda}^{(-j)}(x_i) = y_i \,\right]

Tuning is its argmax over the search set:

λ=argmaxλΛ  CV(λ)\lambda^{*} = \arg\max_{\lambda \in \Lambda} \; \mathrm{CV}(\lambda)

Grid: Λ\Lambda is the lattice. Random: λtp(λ)\lambda_t \sim p(\lambda), log-uniform for scale knobs.

Watch the search find the sweet spot

Grid crawls all 25 cells (star = best so far). Random scatters the same budget — 12 draws — and lands near the peak in a fraction of the fits.

Grid's best: 0.781 in 25 fits. Random: 0.778 by draw 4, done in 12. On two knobs, modest. On ten, a chasm.

The validation curve

Fix C, sweep gamma. Cyan = held-out CV, orange = training folds. The peak is the sweet spot; the gap is the overfitting.

Small gamma: both low, high bias. Large gamma: training → 1.0, validation collapses. Peak ≈ 0.003.

The golden rule: nest

Tune on 576 training patients with inner CV. Report on 192 held-out patients, touched once.

CV: 0.774 → 0.781, robust. Test: 0.734 → 0.776 (192 points — trust the CV, glance at the test).

Takeaways