Decision Tree Classification and Visualization using graphviz

I found that Graphviz is useful to explain the classification process to the customers.

In order to install and run graphviz on python, you need to go to the distribution website and install package for Windows or Linux.

https://graphviz.org/download/

Once package is installed, the execute path needs to be added to the environmental variable path and load them into the code

os.environ["PATH"] += os.pathsep + 'C:\Program Files\Graphviz/bin/'
graphviz env
graphviz env
#%%

import graphviz
from sklearn.datasets import load_wine
from sklearn.tree import DecisionTreeClassifier
import os

os.environ["PATH"] += os.pathsep + 'C:\Program Files\Graphviz/bin/'


wine = load_wine()
X = wine.data
y = wine.target
clf = DecisionTreeClassifier(max_depth = 2)
clf.fit(X, y)


from dtreeviz.trees import dtreeviz # remember to load the package

viz = dtreeviz(clf, X, y,
                target_name="target",
                feature_names=wine.feature_names,
                class_names=list(wine.target_names))

viz
# %%

dtree render
dtree render

Leave a Reply

Your email address will not be published. Required fields are marked *