Can I extract the underlying decision-rules (or 'decision paths') from a trained tree in a decision tree as a textual list?
Something like:
if A>0.4 then if B<0.2 then if C>0.8 then class='X'
Thanks for your help.
Can I extract the underlying decision-rules (or 'decision paths') from a trained tree in a decision tree as a textual list?
Something like:
if A>0.4 then if B<0.2 then if C>0.8 then class='X'
Thanks for your help.
There is a new
DecisionTreeClassifier
method,decision_path
, in the 0.18.0 release. The developers provide an extensive (well-documented) walkthrough.The first section of code in the walkthrough that prints the tree structure seems to be OK. However, I modified the code in the second section to interrogate one sample. My changes denoted with
# <--
Edit The changes marked by
# <--
in the code below have since been updated in walkthrough link after the errors were pointed out in pull requests #8653 and #10951. It's much easier to follow along now.Change the
sample_id
to see the decision paths for other samples. I haven't asked the developers about these changes, just seemed more intuitive when working through the example.You can also make it more informative by distinguishing it to which class it belongs or even by mentioning its output value.
Just because everyone was so helpful I'll just add a modification to Zelazny7 and Daniele's beautiful solutions. This one is for python 2.7, with tabs to make it more readable:
You can see a digraph Tree. Then,
clf.tree_.feature
andclf.tree_.value
are array of nodes splitting feature and array of nodes values respectively. You can refer to more details from this github source.This builds on @paulkernfeld 's answer. If you have a dataframe X with your features and a target dataframe y with your resonses and you you want to get an idea which y value ended in which node (and also ant to plot it accordingly) you can do the following:
not the most elegant version but it does the job...
Here is a function, printing rules of a scikit-learn decision tree under python 3 and with offsets for conditional blocks to make the structure more readable: