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.
Apparently a long time ago somebody already decided to try to add the following function to the official scikit's tree export functions (which basically only supports export_graphviz)
Here is his full commit:
https://github.com/scikit-learn/scikit-learn/blob/79bdc8f711d0af225ed6be9fdb708cea9f98a910/sklearn/tree/export.py
Not exactly sure what happened to this comment. But you could also try to use that function.
I think this warrants a serious documentation request to the good people of scikit-learn to properly document the
sklearn.tree.Tree
API which is the underlying tree structure thatDecisionTreeClassifier
exposes as its attributetree_
.I've been going through this, but i needed the rules to be written in this format
So I adapted the answer of @paulkernfeld (thanks) that you can customize to your need
Here is my approach to extract the decision rules in a form that can be used in directly in sql, so the data can be grouped by node. (Based on the approaches of previous posters.)
The result will be subsequent
CASE
clauses that can be copied to an sql statement, ex.SELECT COALESCE(*CASE WHEN <conditions> THEN > <NodeA>*, > *CASE WHEN <conditions> THEN <NodeB>*, > ....)NodeName,* > FROM <table or view>
Here is a way to translate the whole tree into a single (not necessarily too human-readable) python expression using the SKompiler library:
I modified the code submitted by Zelazny7 to print some pseudocode:
if you call
get_code(dt, df.columns)
on the same example you will obtain:Scikit learn introduced a delicious new method called
export_text
in version 0.21 (May 2019) to extract the rules from a tree. Documentation here. It's no longer necessary to create a custom function.Once you've fit your model, you just need two lines of code. First, import
export_text
:Second, create an object that will contain your rules. To make the rules look more readable, use the
feature_names
argument and pass a list of your feature names. For example, if your model is calledmodel
and your features are named in a dataframe calledX_train
, you could create an object calledtree_rules
:Then just print or save
tree_rules
. Your output will look like this: