Grid Search with Cross-Validation

medium
Unit 12 · Ensembles & Model Selection0/3 to milestone

Pick hyperparameters by trying every combination and scoring each with k-fold cross-validation.

Implement:

  • param_grid(grid): expand a dict of lists into a list of dicts covering every combination. Keys are iterated in sorted order and values in the given order, with the last key varying fastest.
  • kfold_indices(n, k): a list of (train_idx, val_idx) NumPy arrays, using contiguous folds where the first n % k folds get one extra item
  • grid_search(fit_score, grid, X, y, k=5): fit_score(params, X_tr, y_tr, X_val, y_val) returns a score (higher is better). Return (best_params, results), where results is a list of (params, mean_score) in grid order. Ties go to the earliest combination.
#model-selection#cross-validation#hyperparameters2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Grid Search with Cross-Validation · TechGradify