Visualize Decision Tree in 5 Lines of Code

Original article was published on Artificial Intelligence on Medium

Visualize Decision Tree in 5 Lines of Code

Note:

The important library for decision tree visualization is Graphviz.
Make sure it is installed on your machine and if you are using collab or Kaggle notebooks this library is already installed.

The complete code is on my GitHub and here’s the link.Replace the data with your own data set and boom you are good to go and visualize the decision tree.

Code Explanation

Step 1 : Import all the required libraries

Step 2 : Load the data set and split the it for training

Step 3 : Train the Decision Tree model

Step 4 : 5 lines of code for Visualization

NOTE:

The feature names attribute takes names of each of the features and class names attribute takes names of each of the target classes in ascending numerical order. Only relevant for classification and not supported for multi-output. If True, shows a symbolic representation of the class name.

Here’s the final result

The final result will be saved as a pdf file.

More resources:

If you want to learn more about Decision Tree click here

Conclusion

Finally you’ve learnt how to build a decision tree and visualize in 5 lines of code. A clap and follow would be appreciated.

I can be reached on linkedin Davuluri Hemanth Chowdary.