CommencerCommencer gratuitement

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.

Cet exercice fait partie du cours

Machine Learning for Marketing in Python

Afficher le cours

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 from graphviz and pass the exported graphviz object.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de 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")
Modifier et exécuter le code