Eliminating the need for nested for-loops to get hyperparameter combinations

For this exercise, if we first make all the possible combinations of the hyperparameters into a list, then we can loop through this list only once instead of using two for loops—which I find confusing. We can make such a list via itertools.product function.

image

Then use it like this:

for lr, mln in hyperparam_comb:
    model.set_params(
        classifier__learning_rate=lr, 
        classifier__max_leaf_nodes=mln)
    ...

According to the documentation, the output of itertools.product would be the same as using nested for-loops in a generator expression:

image

2 Likes

This is indeed equivalent, thanks for sharing this alternative :slight_smile: For the sake of the exercise we tried to keep code conceptually simple and the main idea that we pass in the following notebook is that a GridSearchCV will do the trick internally.

2 Likes