Exporting the tree

In Decision Tree classification tasks, overfitting is usually the result of deeply grown trees. As the comparison of accuracy scores on the train and test sets shows, you have overfitting in your results. This can also be learned from the tree visualization.

In this exercise, you will export the decision tree into a text document, which can then be used for visualization.

This exercise is part of the course

HR Analytics: Predicting Employee Churn in Python

View Course

Exercise instructions

  • Import the the export_graphviz() function from the the sklearn.tree submodule.
  • Fit the model to the training data.
  • Export the visualization to the file tree.dot.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import the graphical visualization export function
from sklearn.____ import export_graphviz

# Apply Decision Tree model to fit Features to the Target
model.____(features_train,target_train)

# Export the tree to a dot file
____(model,"tree.____")