开始使用免费开始使用

融会贯通

您刚加入一家心律失常检测初创公司,计划在心律失常数据集 arrh 上训练一个模型。您注意到随机森林在不少 Kaggle 比赛中表现出色,因此想用网格搜索来尝试最大深度为 2、5 或 10 的设置。您还观察到数据集维度较高,希望评估一种特征选择方法的效果。

为避免无意中过拟合,您已经完成了数据划分。您将使用 X_trainy_train 进行网格搜索,并使用 X_testy_test 来判断特征选择是否有帮助。四个数据折都已预加载到您的环境中。您还能使用 GridSearchCV()train_test_split()SelectKBest()chi2(),以及作为 rfc 提供的 RandomForestClassifier

本练习是课程的一部分

用 Python 设计机器学习工作流

查看课程

练习说明

  • 使用网格搜索,针对 RandomForestClassifier 的最大深度 2、5、10 进行实验,并保存表现最好的参数设置。
  • 然后使用上述选出的最佳参数重新拟合估计器。
  • 应用基于 chi2 评分函数的 SelectKBest 特征选择器,并重新拟合分类器。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Find the best value for max_depth among values 2, 5 and 10
grid_search = GridSearchCV(
  ____(random_state=1), param_grid=____)
best_value = grid_search.____(
  ____, ____).best_params_['max_depth']

# Using the best value from above, fit a random forest
clf = rfc(
  random_state=1, ____=best_value).____(X_train, y_train)

# Apply SelectKBest with chi2 and pick top 100 features
vt = SelectKBest(____, k=____).____(X_train, y_train)

# Create a new dataset only containing the selected features
X_train_reduced = ____.transform(____)
编辑并运行代码