开始使用免费开始使用

拆解决策树规则

在本练习中,您将从决策树中提取 if-else 规则,并将其可视化,以找出影响流失的主要因素。

训练好的决策树实例已加载为 mytree,经过缩放的特征已作为 pandas DataFrame 加载为 train_Xsklearn 库中的 tree 模块和 graphviz 库也已为您加载。

请注意,我们使用了专有的 display_image() 函数来替代 display(graph),以便您更方便地查看输出。

本练习是课程的一部分

Python 营销中的机器学习

查看课程

练习说明

  • 从已训练的决策树中导出 graphviz 对象。
  • 指定特征名称。
  • 将精度设置为 1,并添加类别名称。
  • 调用 graphvizSource() 函数,并传入导出的 graphviz 对象。

交互式实操练习

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

# Export graphviz object from the trained decision tree 
exported = tree.___(decision_tree=mytree, 
			# Assign feature names
            out_file=None, ___=train_X.columns, 
			# Set precision to 1 and add class names
			precision=1, ___=['Not churn','Churn'], filled = True)

# Call the Source function and pass the exported graphviz object
graph = graphviz.___(exported)

# Display the decision tree
display_image("/usr/local/share/datasets/decision_tree_rules.png")
编辑并运行代码