Skip to content
Advertisement

How to retrieve the full branch path leading to each leaf node of a sklearn Decision Tree?

I have this decision tree, which I would like to extract every branch from it. The image is a portion of the tree, since the original tree is much bigger but it doesn’t fit well on a single image.

enter image description here

I’m not trying to print the rules of the tree like

JavaScript

or like:

JavaScript

What I’m trying to achieve is something like:

JavaScript

Basically, I’m trying to obtain every branch from the top to the leaf node (the full path) with the class and the gini score. How can I achieve this?

Advertisement

Answer

Considering the irist dataset example from sklearn docs we follow the next steps.

1.Generate an example Decision Tree

Code taken from docs

JavaScript

2. Retrieve branches paths

First we retrieve the following values from the tree

JavaScript

Inside retrieve_branches we calculate the leaf nodes and iterate from the origin node down to the leaf nodes, when we get to a leaf node we return the branch path with a yield statement.

JavaScript

To call the retrieve_branches just pass n_nodes, children_left and children_right and an empty list that will be storing and updating the branches paths. Final display is shown below.

JavaScript

3. Path, value and Gini by Branch

The rules can be obtain from the feature and threshold values of the clf.tree_, as well as the impurity clf.tree_.impurity and the values clf.tree_.value at the leaf node.

JavaScript
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement