使用随机森林进行随机搜索
为了巩固您对随机抽样的理解,我们来做一道类似的练习,但使用不同的超参数和不同的算法。
与之前一样,先创建一些超参数列表,并打包成「列表的列表」。您将使用随机森林算法的超参数 criterion、max_depth 和 max_features。然后,随机抽取超参数组合,为后续的随机搜索做准备。
本次任务中,您将使用一个稍有不同的抽样函数:random.sample()。
本练习是课程的一部分
Python 中的超参数调优
练习说明
- 为
criterion创建取值列表'gini'和'entropy',为max_features创建取值列表"auto", "sqrt", "log2", None。 - 为超参数
max_depth创建一个包含 3 到 55(含)之间取值的列表,并赋给max_depth_list。请记住,range(N,M)会生成从N到M-1 的序列。 - 使用
product()将这些列表组合为一个可供抽样的「列表的列表」。 - 从组合后的列表中随机抽取 150 个模型,并打印结果。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# 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(____)