Get startedGet started for free

Randomly Search with Random Forest

To solidify your knowledge of random sampling, let's try a similar exercise but using different hyperparameters and a different algorithm.

As before, create some lists of hyperparameters that can be zipped up to a list of lists. You will use the hyperparameters criterion, max_depth and max_features of the random forest algorithm. Then you will randomly sample hyperparameter combinations in preparation for running a random search.

You will use a slightly different package for sampling in this task, random.sample().

This exercise is part of the course

Hyperparameter Tuning in Python

View Course

Exercise instructions

  • Create lists of the values 'gini' and 'entropy' for criterion & "auto", "sqrt", "log2", None for max_features.
  • Create a list of values between 3 and 55 inclusive for the hyperparameter max_depth and assign to the list max_depth_list. Remember that range(N,M) will create a list from N to M-1.
  • Combine these lists into a list of lists to sample from using product().
  • Randomly sample 150 models from the combined list and print the result.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Create lists for criterion and max_features
criterion_list = ____
max_feature_list = ____

# Create a list of values for the max_depth hyperparameter
max_depth_list = list(range(____,____))

# Combination list
combinations_list = [list(x) for x in product(____, ____, ____)]

# Sample hyperparameter combinations for a random search
combinations_random_chosen = random.sample(____, ____)

# Print the result
print(____)
Edit and Run Code