03-bias-variance-tradeoff shows that a model's complexity — its hyperparameters — controls where it lands on the bias/variance tradeoff, and that neither extreme wins. But nothing so far in this curriculum has picked those hyperparameters systematically; learning_rate, max_depth, n_trees, lam have all been chosen by hand, by the person writing the training call, not discovered from the data.
The most literal fix: define a small set of candidate values for each hyperparameter, actually try every combination of them by training and validating a real model for each one, and keep whichever combination scored best. No cleverness, no assumptions about which hyperparameters matter more — just brute-force coverage of a defined search space.
Implement generate_param_combinations(param_grid) to enumerate every combination, then grid_search(param_grid, fit_and_score_fn) to score each one and track the winner. fit_and_score_fn is a black box supplied by the caller — it trains a model with one specific set of hyperparameters and returns a validation score, higher is better; this question doesn't care what model it is, just how to search over its hyperparameters.
param_grid: a dict mapping each hyperparameter name to a list of candidate values.generate_param_combinations returns a list of dicts, one per combination — the Cartesian product of every hyperparameter's candidate values, e.g. {"lr": [0.01, 0.1], "depth": [2, 3]} produces 4 combinations.grid_search returns a dict with "best_params" (the params dict that scored highest), "best_score" (that score), and "all_scores" (a list of (params, score) for every combination tried, in the order tried).fit_and_score_fn is called exactly once per combination.Open one at a time. Each gives away a little more than the last.
The standard library already has a Cartesian product: itertools.product(*value_lists) over param_grid's values, one list per hyperparameter.
Each tuple itertools.product yields needs to become a dict again — dict(zip(keys, values)) pairs it back up with param_grid's original keys, in the same order they were passed to product.
No results yet
Run your code to check it against the examples, or submit to run every test.