Explore Courses Blog Tutorials Interview Questions
0 votes
in Machine Learning by (33.1k points)

Is there a way to plot a decision tree in a Jupyter Notebook, such that I can interactively explore its nodes? I am thinking about something like this 


This is an example from KNIME.

I have found and and I know you can run d3 in Jupyter, but I have not found any packages, that do that.

1 Answer

0 votes
by (33.1k points)

You can easily plot a graph for the Desicion tree. This can be done by the PYDot library in python.

Code to draw a graph using PYDot:


from pydot import Dot, Edge

        g = Dot()

        g.set_node_defaults(color='lightgray', style='filled', shape='box',

       fontname='Courier', fontsize='10')

       for node in sorted(self.nodes, key=lambda x: x.num):

            if draw_branches and node.type.is_cond:

                g.add_edge(Edge(str(node), str(node.true), color='green'))

                g.add_edge(Edge(str(node), str(node.false), color='red'))


        for suc in self.sucs(node):

                    g.add_edge(Edge(str(node), str(suc), color='blue'))

            for except_node in self.catch_edges.get(node, []):

                g.add_edge(Edge(str(node), str(except_node),

                                color='black', style='dashed'))

        g.write_png('%s/%s.png' % (dname, name))

The code will return graph similar to the following graph:


Hope this answer helps.

If you want to learn more about Python then visit this Python Course.

Welcome to Intellipaat Community. Get your technical queries answered by top developers!

29.3k questions

30.6k answers


104k users

Browse Categories