Break down decision tree rules
In this exercise you will extract the if-else rules from the decision tree and plot them to identify the main drivers of the churn.
The fitted decision tree instance is loaded as mytree
and the scaled features are loaded as a pandas
DataFrame called train_X
. The tree
module from sklearn
library and the graphviz
library have been already loaded for you.
Note that we've used a proprietary display_image()
function instead of display(graph)
to make it easier for you to view the output.
This exercise is part of the course
Machine Learning for Marketing in Python
Exercise instructions
- Export the
graphviz
object from the trained decision tree . - Assign the feature names.
- Set the precision to 1 and add the class names.
- Call the
Source()
function fromgraphviz
and pass the exportedgraphviz
object.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# 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")