Decision tree visual example
A decision tree can be visualized. A decision tree is one of the many Machine Learning algorithms.
It’s used as classifier: given input data, it is class A or class B? In this lecture we will visualize a decision tree using the Python module pydotplus and the module graphviz
If you want to do decision tree analysis, to understand the decision tree algorithm / model or if you just need a decision tree maker - you’ll need to visualize the decision tree.
You need to install pydotplus and graphviz. These can be installed with your package manager and pip.
Graphviz is a tool for drawing graphics using dot files. Pydotplus is a module to Graphviz’s Dot language.
We start by defining the code and data collection. Let’s make the decision tree on man or woman. Given input features: “height, hair length and voice pitch” it will predict if its a man or woman.
In code that looks like:
The next step is to train the classifier (decision tree) with the training data.
Training is always necessary for supervised learning algorithms
Decision Tree Visualization
We then visualize the tree using this complete code:
# Visualize data
This will save the visualization to the image tree.png, which looks like this:
If you want to make predictions, check out the decision tree article.