0

The answer of this question can highlight the path of a decision tree, but is there any way to only plot a single path of a decision tree in python? My decision tree is built by tree.DecisionTreeClassifier() in scikit-learn and visualized by Graphviz as follows:

dot_data = tree.export_graphviz(clf, out_file=None,
                 feature_names=iris.feature_names,
                 class_names=iris.target_names,
                 filled=True, rounded=True,
                 special_characters=True)
graph = graphviz.Source(dot_data)

The reason for doing this is when the decision tree is deep, there will be a large number of nodes and the tree is too narrow to be used as a figure of an article.

This action can be done by manually editing the dot_data file generated by the function export_graphviz(), i.e., manually deleting the lines of the dot_data file as follows and leave the lines representing the path I want to plot.

digraph Tree {
node [shape=box, style="filled, rounded", color="black", fontname="helvetica"] ;
edge [fontname="helvetica"] ;
0 [label=<petal width (cm) &le; 0.8<br/>gini = 0.667<br/>samples = 150<br/>value = [50, 50, 50]<br/>class = setosa>, fillcolor="#ffffff"] ;
1 [label=<gini = 0.0<br/>samples = 50<br/>value = [50, 0, 0]<br/>class = setosa>, fillcolor="#e58139"] ;
...

This action can surely be done by python, but is there any simple way to do this? For example, is it possible to change some parameters of the existing functions to plot a single path of a decision tree in python? Thanks for helping!

  • If you can think of how to do it by hand, there's likely a way to do it in Python - what have you tried yourself? Please share code of your best attempt so far and explain where you got stuck. – Grismar May 30 '22 at 00:25
  • @Grismar Thanks for your suggestion! I have edit the question according to your valuable suggestion! – Xiaoshi Wang Jun 04 '22 at 01:41

0 Answers0