开始使用免费开始使用

提取随机森林的参数

现在,您将把之前在逻辑回归模型上的工作迁移到随机森林模型上。该模型的一个参数是:对于给定的树,它在每一层如何进行划分。

这一分析不如逻辑回归的系数那样直接有用,因为在随机森林模型中,您几乎不可能逐一查看每一次划分和每一棵树。不过,这依然是一个很好的练习,有助于您窥探模型在"引擎盖下"到底做了什么。

在本练习中,我们将从随机森林模型中提取一棵单独的树,对其进行可视化,并通过编程方式提取其中一个划分。

您可以使用:

  • 随机森林模型对象 rf_clf
  • 选定决策树顶部的图像 tree_viz_image
  • X_train DataFrame 与 original_variables 列表

本练习是课程的一部分

Python 中的超参数调优

查看课程

练习说明

  • 从随机森林模型中提取第 7 棵树(索引为 6)。
  • 可视化该树(tree_viz_image)以查看划分决策。
  • 提取顶部划分的特征与阈值。
  • 将该特征和阈值一起打印出来。

交互式实操练习

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

# Extract the 7th (index 6) tree from the random forest
chosen_tree = rf_clf.estimators_[____]

# Visualize the graph using the provided image
imgplot = plt.imshow(____)
plt.show()

# Extract the parameters and level of the top (index 0) node
split_column = chosen_tree.tree_.feature[____]
split_column_name = X_train.columns[split_column]
split_value = chosen_tree.tree_.threshold[____]

# Print out the feature and level
print("This node split on feature {}, at a value of {}".format(split_column_name, ____))
编辑并运行代码