使用 TPOT 进行遗传式超参数调优
您将进行一个遗传式超参数调优的简单示例。TPOT 是一个功能非常强大的库,具备众多特性。本课只是入门示例,强烈建议您在课后自行进一步探索。
这是一个很小的示例。在真实场景中,TPOT 往往需要运行数小时来寻找最优模型。您通常会使用更大的种群规模与后代数量,并迭代数百代来获得更好的模型。
您将创建估计器,将其拟合到训练数据,然后在测试数据上进行评分。
在本示例中我们希望使用:
- 3 个世代(generations)
- 种群规模为 4
- 每代产生 3 个后代(offspring)
- 以 accuracy 作为评分指标
为保证结果一致性,random_state 已设置为 2。
本练习是课程的一部分
Python 中的超参数调优
练习说明
- 按照上下文给出的取值,为
tpot_clf的各输入参数赋值。 - 使用正确的输入参数创建分类器
tpot_clf。 - 将分类器拟合到训练数据(工作区中已提供
X_train与y_train)。 - 使用已拟合的分类器在测试集上评分(工作区中已提供
X_test与y_test)。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Assign the values outlined to the inputs
number_generations = ____
population_size = ____
offspring_size = ____
scoring_function = ____
# Create the tpot classifier
tpot_clf = TPOTClassifier(generations=____, population_size=____,
offspring_size=____, scoring=____,
verbosity=2, random_state=2, cv=2)
# Fit the classifier to the training data
____.____(____, ____)
# Score on the test set
print(____.____(____, ____))