LoslegenKostenlos loslegen

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.

Diese Übung ist Teil des Kurses

Machine Learning for Marketing in Python

Kurs anzeigen

Anleitung zur Übung

  • 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.

Interaktive Übung

Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.

# 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")
Code bearbeiten und ausführen