Visualization issue of dtreeviz framework

Question:

here is my code run in pycharm and it is supposed to display nice visualization chart, but no result is shown

import matplotlib.pyplot as plt
import pydotplus
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_iris
from sklearn import tree
from io import StringIO
from ipywidgets import Image
from dtreeviz.trees import *
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree import plot_tree
iris = load_iris()
X = iris.data
y = iris.target
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# fit the classifier
clf = DecisionTreeClassifier(max_depth=3, random_state=42)
clf.fit(X_train, y_train)
viz = dtreeviz(clf,
               x_data=X_train,
               y_data=y_train,
               target_name='class',
               feature_names=iris.feature_names,
               class_names=list(iris.target_names),
               title="Decision Tree - Iris data set")
plt.show()

for example i am using this link for reference
https://github.com/erykml/medium_articles/blob/master/Machine%20Learning/decision_tree_visualization.ipynb
even i run this code in google colab, no result is shown in google colab too, so what is wrong? output of this code is just this:

Process finished with exit code 0
Asked By: Machine_Learning

||

Answers:

Your code is mixing some parts of the example code (and is including some parts that aren’t needed).

If you run the below:

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from dtreeviz.trees import dtreeviz
from sklearn.tree import DecisionTreeClassifier

iris = load_iris()
x = iris.data
y = iris.target
x_train, _, y_train, _ = train_test_split(x, y, test_size=0.2, random_state=42)

clf = DecisionTreeClassifier(max_depth=3, random_state=42)
clf.fit(x_train, y_train)

viz = dtreeviz(clf,
               x_data=x_train,
               y_data=y_train,
               target_name='class',
               feature_names=iris.feature_names,
               class_names=list(iris.target_names),
               title="Decision Tree - Iris data set")
viz.view()

And providing you have all the referenced packages installed, as well as GraphViz and GraphViz was configured to be on the PATH (before you started your editor / IDE), then calling viz.view() will generate and launch a .svg file.

If you have an application configured for viewing .svg (like a web browser, or InkScape, etc.) it should show the result you’re after.

(Note that I also ran this code from PyCharm, on Python 3.10.8 – any 3.10 should work – using a recent version of all the packages and a fresh install of GraphViz.)

You can check if GraphViz can be run by opening a terminal in PyCharm and running: dot -V. You should see something like:

dot - graphviz version 6.0.2 (20221011.1828)
Answered By: Grismar