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
Related course: Machine Learning A-Z: Hands-On Python & R In Data Science
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:
from sklearn.datasets import load_iris
from sklearn import tree
# Data Collection
X = [ [180, 15,0],
Y = ['man', 'woman', 'woman', 'man', 'woman']
data_feature_names = [ 'height', 'hair length', 'voice pitch' ]
The next step is to train the classifier (decision tree) with the training data.
Training is always necessary for supervised learning algorithms
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X,Y)
Decision Tree Visualization
We then visualize the tree using this complete code:
# Visualize data
dot_data = tree.export_graphviz(clf,
graph = pydotplus.graph_from_dot_data(dot_data)
colors = ('turquoise', 'orange')
edges = collections.defaultdict(list)
for edge in graph.get_edge_list():
for edge in edges:
for i in range(2):
dest = graph.get_node(str(edges[edge][i]))
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.