Skip to content

[MRG] Print Decision Trees in ASCII format - #9424

Merged
jnothman merged 68 commits into
scikit-learn:masterfrom
JustGlowing:decision-tree-print-function
Feb 11, 2019
Merged

jnothman merged 68 commits into
scikit-learn:masterfrom
JustGlowing:decision-tree-print-function

Conversation

@JustGlowing

@JustGlowing JustGlowing commented Jul 20, 2017 •

Copy link
Copy Markdown
Contributor

What does this implement/fix? Explain your changes.

I noticed that the only way to visually check the rules of a Decision Tree was to export the tree in dot format then use Graph Viz. This process is a bit tedious and may be impossible on some machines. So, I decided to implement a little method to print the tree in ASCII:

from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
from sklearn.tree.export import export_ascii
iris = load_iris()
X = iris['data']
y = iris['target']
decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
decision_tree.fit(X, y)
print(export_ascii(decision_tree, feature_names=iris['feature_names'],
				   class_names=iris['target_names'], show_class=True))
|---petal width (cm) <= 0.80
|   | (class: setosa)
|   |---* value: [ 50.   0.   0.]
|   |   | (class: setosa)
|---petal width (cm) >  0.80
|   | (class: setosa)
|   |---petal width (cm) <= 1.75
|   |   | (class: versicolor)
|   |   |---* value: [  0.  49.   5.]
|   |   |   | (class: versicolor)
|   |---petal width (cm) >  1.75
|   |   | (class: versicolor)
|   |   |---* value: [  0.   1.  45.]
|   |   |   | (class: virginica)

Notes:

@glemaitre

Copy link
Copy Markdown
Member

I think that there is a pure matplotlib PR. Not sure what is the best

@JustGlowing

Copy link
Copy Markdown
Contributor Author

@glemaitre with matplotlib you have a rich output but another library needs to be added in the dependencies. With ASCII the output is less attractive but no dependencies are required. I'd like to have both features.

@jmschrei

Copy link
Copy Markdown
Member

I am +1 on having another method for inspecting trees. However, one will have to consider the case when you get verrrrry deep trees (50+ layers, 100+ layers...) what the appropriate thing to do is. Potentially, this would be limited to either only printing out small trees or printing out the first few layers of the trees.

@agramfort

agramfort commented Jul 20, 2017 via email

Copy link
Copy Markdown
Member

@amueller

Copy link
Copy Markdown
Member

There's a PR to only use matplotlib and not dot and graphviz: #9251

@amueller

Copy link
Copy Markdown
Member

The other methods have a maximum depth parameter to plot only the top of large trees.
Maybe check out my Pr for a refactoring that might allow this to be done more easily (though it is quite short now).
My Pr also has a tree layout algorithm we could use for ascii as well, but that would complicate the code somewhat.

@amueller

amueller commented Jul 20, 2017 •

Copy link
Copy Markdown
Member

We probably don't want this as a method for now, right? It should be a function, I think.

@agramfort

agramfort commented Jul 21, 2017 via email

Copy link
Copy Markdown
Member

@jmschrei

Copy link
Copy Markdown
Member

It should follow the format of the other inspection techniques, so a function might be more appropriate.

@JustGlowing

Copy link
Copy Markdown
Contributor Author

I've moved the technique in a function and added a bunch of parameters to make it more flexible.

To handle large tree I added two parameters max_depth and spacing. max_depth controls the number of layers printed and spacing the width of the indentation. So one can have a large spacing for a small tree:

|-----petal width (cm) <= 0.80
|     | (value: [ 50.  50.  50.])
|     | (class: 0)
|-----petal width (cm) >  0.80
|     | (value: [ 50.  50.  50.])
|     | (class: 0)

and a small spacing for a large one:

|-petal width (cm) <= 0.80
| | (value: [ 50.  50.  50.])
| | (class: 0)
| |-* (value: [ 50.   0.   0.])
| | | (class: 0)
|-petal width (cm) >  0.80
| | (value: [ 50.  50.  50.])
| | (class: 0)
| |-petal width (cm) <= 1.75
| | | (value: [  0.  50.  50.])
| | | (class: 1)
| | |-* (value: [  0.  49.   5.])
| | | | (class: 1)
| |-petal width (cm) >  1.75
| | | (value: [  0.  50.  50.])
| | | (class: 1)
| | |-* (value: [  0.   1.  45.])
| | | | (class: 2)

One can also decide not to print value and class for each to get a more compact result:

|-petal width (cm) <= 0.80
| |-* (value: [ 50.   0.   0.])
|-petal width (cm) >  0.80
| |-petal width (cm) <= 1.75
| | |-* (value: [  0.  49.   5.])
| |-petal width (cm) >  1.75
| | |-* (value: [  0.   1.  45.])

@JustGlowing

Copy link
Copy Markdown
Contributor Author

I'll write a comment describing the last changes in the following days. Sorry for closing the PR, I hit the the wrong button.

@JustGlowing

Copy link
Copy Markdown
Contributor Author

hi @jnothman and thanks for your comments.

From your observations I realized that it was too easy to make the result unreadable due to too many parameters and too many items reported. So I made the following changes:

  1. removed the parameters show_value and show_class. Now value and class are shown only on leaves and in a compact way (just one line as you suggested).
  2. introduced the parameter decimals so that the user can tweak how many decimal digits to print as in some cases decimals occupy most of the output.

I also created this script to check the output in two specific cases with reasonable parameters:

from sklearn.datasets import load_iris, load_boston
from sklearn.tree import DecisionTreeClassifier, DecisionTreeRegressor
from sklearn.tree.export import export_text

print('*** CLASSIFICATION EXAMPLE (IRIS DATASET) ***')
iris = load_iris()
X = iris['data']
y = iris['target']
y_str = [iris['target_names'][0]]*50 + [iris['target_names'][1]]*50 + [iris['target_names'][2]]*50
decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
decision_tree.fit(X, y_str)
r = export_text(decision_tree, feature_names=iris['feature_names'], decimals=2)
print(r)

print('*** REGRESSION EXAMPLE (BOSTON DATASET) ***')
boston = load_boston()
X = boston['data']
y = boston['target']
regressor = DecisionTreeRegressor(max_depth=3, random_state=0)
regressor.fit(X, y)
print(export_text(regressor, feature_names=list(boston['feature_names']), decimals=2))

Here's the result:

*** CLASSIFICATION EXAMPLE (IRIS DATASET) ***
|---petal width (cm) <= 0.80
|   |--- weights: [50.00, 0.00, 0.00] -> setosa
|---petal width (cm) >  0.80
|   |---petal width (cm) <= 1.75
|   |   |--- weights: [0.00, 49.00, 5.00] -> versicolor
|   |---petal width (cm) >  1.75
|   |   |--- weights: [0.00, 1.00, 45.00] -> virginica

*** REGRESSION EXAMPLE (BOSTON DATASET) ***
|---RM <= 6.94
|   |---LSTAT <= 14.40
|   |   |---DIS <= 1.38
|   |   |   |--- value: [45.58]
|   |   |---DIS >  1.38
|   |   |   |--- value: [22.91]
|   |---LSTAT >  14.40
|   |   |---CRIM <= 6.99
|   |   |   |--- value: [17.14]
|   |   |---CRIM >  6.99
|   |   |   |--- value: [11.98]
|---RM >  6.94
|   |---RM <= 7.44
|   |   |---CRIM <= 7.39
|   |   |   |--- value: [33.35]
|   |   |---CRIM >  7.39
|   |   |   |--- value: [14.40]
|   |---RM >  7.44
|   |   |---RM <= 8.75
|   |   |   |--- value: [45.90]
|   |   |---RM >  8.75
|   |   |   |--- value: [21.90]

Now the default parameter get a decent result most of the times and the code is a simpler.

Have a look when you have time ;-)

@rth rth left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like this. Thanks for continuing your work on it PR @JustGlowing !

A few very minor comments below.

One major comment I have is that when the tree is fairly deep, because the weights are printed in classification the output can become quite large. See example below,

Details
In [5]: from sklearn.tree import DecisionTreeClassifier 
   ...: from sklearn.tree import export_text 
   ...: from sklearn.datasets import load_digits 
   ...:  
   ...: digits = load_digits() 
   ...:  
   ...: X = digits.data 
   ...: y = digits.target 
   ...:  
   ...: clf = DecisionTreeClassifier(max_depth=10, random_state=0) 
   ...: clf.fit(X, y) 
   ...: print(export_text(clf))                                                                                                                
|---feature_36 <= 0.50
|   |---feature_28 <= 2.50
|   |   |---feature_21 <= 0.50
|   |   |   |---feature_6 <= 1.00
|   |   |   |   |---feature_29 <= 1.00
|   |   |   |   |   |---feature_44 <= 8.00
|   |   |   |   |   |   |---feature_37 <= 0.50
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |   |---feature_37 >  0.50
|   |   |   |   |   |   |   |--- weights: [1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 0
|   |   |   |   |   |---feature_44 >  8.00
|   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 3.00, 0.00, 0.00, 0.00] -> 6
|   |   |   |   |---feature_29 >  1.00
|   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 5.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |---feature_6 >  1.00
|   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 6.00, 0.00, 0.00, 0.00, 0.00] -> 5
|   |   |---feature_21 >  0.50
|   |   |   |---feature_18 <= 0.50
|   |   |   |   |--- weights: [0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |---feature_18 >  0.50
|   |   |   |   |---feature_43 <= 14.50
|   |   |   |   |   |--- weights: [170.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 0
|   |   |   |   |---feature_43 >  14.50
|   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |---feature_28 >  2.50
|   |   |---feature_21 <= 6.50
|   |   |   |---feature_43 <= 0.50
|   |   |   |   |---feature_18 <= 8.50
|   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00] -> 9
|   |   |   |   |---feature_18 >  8.50
|   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 17.00, 0.00, 0.00, 0.00, 0.00] -> 5
|   |   |   |---feature_43 >  0.50
|   |   |   |   |---feature_10 <= 11.50
|   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 2.00, 0.00, 0.00, 0.00] -> 6
|   |   |   |   |---feature_10 >  11.50
|   |   |   |   |   |--- weights: [0.00, 0.00, 2.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |---feature_21 >  6.50
|   |   |   |---feature_34 <= 7.50
|   |   |   |   |---feature_9 <= 10.50
|   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 56.00] -> 9
|   |   |   |   |---feature_9 >  10.50
|   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 3
|   |   |   |---feature_34 >  7.50
|   |   |   |   |---feature_60 <= 11.00
|   |   |   |   |   |---feature_52 <= 10.50
|   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 3.00, 0.00] -> 8
|   |   |   |   |   |---feature_52 >  10.50
|   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 2.00, 0.00, 0.00, 0.00, 0.00] -> 5
|   |   |   |   |---feature_60 >  11.00
|   |   |   |   |   |--- weights: [3.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 0
|---feature_36 >  0.50
|   |---feature_21 <= 0.50
|   |   |---feature_42 <= 8.50
|   |   |   |---feature_5 <= 1.50
|   |   |   |   |---feature_19 <= 13.50
|   |   |   |   |   |---feature_37 <= 4.00
|   |   |   |   |   |   |---feature_58 <= 0.50
|   |   |   |   |   |   |   |---feature_26 <= 7.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 3.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |   |   |   |   |---feature_26 >  7.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00] -> 8
|   |   |   |   |   |   |---feature_58 >  0.50
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 33.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |---feature_37 >  4.00
|   |   |   |   |   |   |---feature_43 <= 5.50
|   |   |   |   |   |   |   |---feature_26 <= 12.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 13.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 3
|   |   |   |   |   |   |   |---feature_26 >  12.50
|   |   |   |   |   |   |   |   |---feature_25 <= 3.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00] -> 9
|   |   |   |   |   |   |   |   |---feature_25 >  3.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 2.00, 0.00, 0.00, 0.00, 0.00] -> 5
|   |   |   |   |   |   |---feature_43 >  5.50
|   |   |   |   |   |   |   |---feature_45 <= 6.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 4.00, 0.00, 0.00] -> 7
|   |   |   |   |   |   |   |---feature_45 >  6.50
|   |   |   |   |   |   |   |   |---feature_46 <= 5.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 2.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |   |   |   |   |   |---feature_46 >  5.00
|   |   |   |   |   |   |   |   |   |---feature_26 <= 10.50
|   |   |   |   |   |   |   |   |   |---feature_26 >  10.50
|   |   |   |   |---feature_19 >  13.50
|   |   |   |   |   |---feature_46 <= 0.50
|   |   |   |   |   |   |---feature_27 <= 10.50
|   |   |   |   |   |   |   |---feature_19 <= 15.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |   |   |---feature_19 >  15.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |   |   |   |---feature_27 >  10.50
|   |   |   |   |   |   |   |--- weights: [0.00, 32.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |   |   |---feature_46 >  0.50
|   |   |   |   |   |   |---feature_33 <= 4.00
|   |   |   |   |   |   |   |---feature_2 <= 2.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 2.00, 0.00, 0.00, 0.00] -> 6
|   |   |   |   |   |   |   |---feature_2 >  2.00
|   |   |   |   |   |   |   |   |---feature_1 <= 2.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 3
|   |   |   |   |   |   |   |   |---feature_1 >  2.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |   |---feature_33 >  4.00
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 2.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |---feature_5 >  1.50
|   |   |   |   |---feature_18 <= 4.50
|   |   |   |   |   |---feature_10 <= 1.00
|   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |   |   |---feature_10 >  1.00
|   |   |   |   |   |   |---feature_14 <= 5.50
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 3
|   |   |   |   |   |   |---feature_14 >  5.50
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00] -> 7
|   |   |   |   |---feature_18 >  4.50
|   |   |   |   |   |---feature_9 <= 15.00
|   |   |   |   |   |   |---feature_24 <= 0.50
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 140.00, 0.00, 0.00, 0.00, 0.00] -> 5
|   |   |   |   |   |   |---feature_24 >  0.50
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |   |   |---feature_9 >  15.00
|   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00] -> 8
|   |   |---feature_42 >  8.50
|   |   |   |---feature_54 <= 1.50
|   |   |   |   |---feature_10 <= 5.50
|   |   |   |   |   |---feature_41 <= 1.50
|   |   |   |   |   |   |---feature_18 <= 5.50
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00] -> 6
|   |   |   |   |   |   |---feature_18 >  5.50
|   |   |   |   |   |   |   |--- weights: [0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |   |   |---feature_41 >  1.50
|   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 24.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |   |---feature_10 >  5.50
|   |   |   |   |   |---feature_45 <= 1.50
|   |   |   |   |   |   |---feature_62 <= 1.50
|   |   |   |   |   |   |   |--- weights: [0.00, 7.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |   |   |   |---feature_62 >  1.50
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 2.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |---feature_45 >  1.50
|   |   |   |   |   |   |---feature_50 <= 6.00
|   |   |   |   |   |   |   |---feature_5 <= 6.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 2.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |   |   |   |   |---feature_5 >  6.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 3.00, 0.00, 0.00, 0.00, 0.00] -> 5
|   |   |   |   |   |   |---feature_50 >  6.00
|   |   |   |   |   |   |   |---feature_38 <= 4.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 6.00, 0.00, 0.00, 0.00] -> 6
|   |   |   |   |   |   |   |---feature_38 >  4.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00] -> 7
|   |   |   |---feature_54 >  1.50
|   |   |   |   |---feature_58 <= 9.00
|   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 162.00, 0.00, 0.00, 0.00] -> 6
|   |   |   |   |---feature_58 >  9.00
|   |   |   |   |   |--- weights: [0.00, 0.00, 9.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |---feature_21 >  0.50
|   |   |---feature_60 <= 7.50
|   |   |   |---feature_5 <= 0.50
|   |   |   |   |---feature_12 <= 7.50
|   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 26.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |   |---feature_12 >  7.50
|   |   |   |   |   |---feature_53 <= 10.50
|   |   |   |   |   |   |---feature_60 <= 1.50
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 6.00, 0.00, 0.00] -> 7
|   |   |   |   |   |   |---feature_60 >  1.50
|   |   |   |   |   |   |   |---feature_26 <= 8.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 3.00, 0.00] -> 8
|   |   |   |   |   |   |   |---feature_26 >  8.50
|   |   |   |   |   |   |   |   |---feature_13 <= 7.50
|   |   |   |   |   |   |   |   |   |---feature_3 <= 14.50
|   |   |   |   |   |   |   |   |   |---feature_3 >  14.50
|   |   |   |   |   |   |   |   |---feature_13 >  7.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 2.00, 0.00, 0.00, 0.00, 0.00] -> 5
|   |   |   |   |   |---feature_53 >  10.50
|   |   |   |   |   |   |---feature_61 <= 12.00
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 5.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |   |---feature_61 >  12.00
|   |   |   |   |   |   |   |--- weights: [0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |---feature_5 >  0.50
|   |   |   |   |---feature_53 <= 3.50
|   |   |   |   |   |---feature_37 <= 0.50
|   |   |   |   |   |   |---feature_52 <= 1.00
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 4.00, 0.00, 0.00] -> 7
|   |   |   |   |   |   |---feature_52 >  1.00
|   |   |   |   |   |   |   |---feature_20 <= 11.50
|   |   |   |   |   |   |   |   |---feature_52 <= 7.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00] -> 9
|   |   |   |   |   |   |   |   |---feature_52 >  7.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 5.00, 0.00] -> 8
|   |   |   |   |   |   |   |---feature_20 >  11.50
|   |   |   |   |   |   |   |   |---feature_1 <= 1.00
|   |   |   |   |   |   |   |   |   |---feature_11 <= 13.00
|   |   |   |   |   |   |   |   |   |---feature_11 >  13.00
|   |   |   |   |   |   |   |   |---feature_1 >  1.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 3.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 3
|   |   |   |   |   |---feature_37 >  0.50
|   |   |   |   |   |   |---feature_26 <= 12.50
|   |   |   |   |   |   |   |---feature_19 <= 12.50
|   |   |   |   |   |   |   |   |---feature_51 <= 2.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 3
|   |   |   |   |   |   |   |   |---feature_51 >  2.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 140.00, 0.00, 0.00] -> 7
|   |   |   |   |   |   |   |---feature_19 >  12.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00] -> 8
|   |   |   |   |   |   |---feature_26 >  12.50
|   |   |   |   |   |   |   |---feature_27 <= 5.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 10.00, 0.00, 0.00] -> 7
|   |   |   |   |   |   |   |---feature_27 >  5.50
|   |   |   |   |   |   |   |   |---feature_33 <= 1.50
|   |   |   |   |   |   |   |   |   |---feature_1 <= 3.00
|   |   |   |   |   |   |   |   |   |---feature_1 >  3.00
|   |   |   |   |   |   |   |   |---feature_33 >  1.50
|   |   |   |   |   |   |   |   |   |---feature_2 <= 6.50
|   |   |   |   |   |   |   |   |   |---feature_2 >  6.50
|   |   |   |   |---feature_53 >  3.50
|   |   |   |   |   |---feature_62 <= 0.50
|   |   |   |   |   |   |---feature_18 <= 1.00
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 6.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 3
|   |   |   |   |   |   |---feature_18 >  1.00
|   |   |   |   |   |   |   |---feature_44 <= 5.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 4.00] -> 9
|   |   |   |   |   |   |   |---feature_44 >  5.50
|   |   |   |   |   |   |   |   |---feature_21 <= 12.00
|   |   |   |   |   |   |   |   |   |---feature_20 <= 9.50
|   |   |   |   |   |   |   |   |   |---feature_20 >  9.50
|   |   |   |   |   |   |   |   |---feature_21 >  12.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 2.00, 0.00] -> 8
|   |   |   |   |   |---feature_62 >  0.50
|   |   |   |   |   |   |--- weights: [0.00, 6.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |---feature_60 >  7.50
|   |   |   |---feature_33 <= 3.50
|   |   |   |   |---feature_43 <= 1.50
|   |   |   |   |   |---feature_29 <= 12.50
|   |   |   |   |   |   |---feature_34 <= 3.50
|   |   |   |   |   |   |   |---feature_26 <= 8.50
|   |   |   |   |   |   |   |   |---feature_28 <= 7.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 2.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |   |   |   |---feature_28 >  7.50
|   |   |   |   |   |   |   |   |   |---feature_19 <= 14.50
|   |   |   |   |   |   |   |   |   |---feature_19 >  14.50
|   |   |   |   |   |   |   |---feature_26 >  8.50
|   |   |   |   |   |   |   |   |---feature_59 <= 13.50
|   |   |   |   |   |   |   |   |   |---feature_26 <= 10.00
|   |   |   |   |   |   |   |   |   |---feature_26 >  10.00
|   |   |   |   |   |   |   |   |---feature_59 >  13.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 3.00, 0.00, 0.00, 0.00, 0.00] -> 5
|   |   |   |   |   |   |---feature_34 >  3.50
|   |   |   |   |   |   |   |---feature_42 <= 3.50
|   |   |   |   |   |   |   |   |---feature_25 <= 2.00
|   |   |   |   |   |   |   |   |   |---feature_2 <= 3.50
|   |   |   |   |   |   |   |   |   |---feature_2 >  3.50
|   |   |   |   |   |   |   |   |---feature_25 >  2.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 5.00] -> 9
|   |   |   |   |   |   |   |---feature_42 >  3.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 14.00, 0.00] -> 8
|   |   |   |   |   |---feature_29 >  12.50
|   |   |   |   |   |   |---feature_35 <= 1.50
|   |   |   |   |   |   |   |---feature_3 <= 3.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 15.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |   |   |   |   |---feature_3 >  3.50
|   |   |   |   |   |   |   |   |---feature_27 <= 7.50
|   |   |   |   |   |   |   |   |   |---feature_19 <= 3.50
|   |   |   |   |   |   |   |   |   |---feature_19 >  3.50
|   |   |   |   |   |   |   |   |---feature_27 >  7.50
|   |   |   |   |   |   |   |   |   |---feature_3 <= 15.50
|   |   |   |   |   |   |   |   |   |---feature_3 >  15.50
|   |   |   |   |   |   |---feature_35 >  1.50
|   |   |   |   |   |   |   |---feature_26 <= 2.50
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 3.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 3
|   |   |   |   |   |   |   |---feature_26 >  2.50
|   |   |   |   |   |   |   |   |---feature_21 <= 10.50
|   |   |   |   |   |   |   |   |   |---feature_51 <= 1.00
|   |   |   |   |   |   |   |   |   |---feature_51 >  1.00
|   |   |   |   |   |   |   |   |---feature_21 >  10.50
|   |   |   |   |   |   |   |   |   |---feature_11 <= 1.00
|   |   |   |   |   |   |   |   |   |---feature_11 >  1.00
|   |   |   |   |---feature_43 >  1.50
|   |   |   |   |   |---feature_27 <= 7.50
|   |   |   |   |   |   |---feature_34 <= 3.50
|   |   |   |   |   |   |   |---feature_50 <= 0.50
|   |   |   |   |   |   |   |   |---feature_12 <= 14.50
|   |   |   |   |   |   |   |   |   |---feature_14 <= 3.50
|   |   |   |   |   |   |   |   |   |---feature_14 >  3.50
|   |   |   |   |   |   |   |   |---feature_12 >  14.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 2.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |   |   |   |   |---feature_50 >  0.50
|   |   |   |   |   |   |   |   |---feature_45 <= 14.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 88.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |   |   |   |---feature_45 >  14.00
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 3
|   |   |   |   |   |   |---feature_34 >  3.50
|   |   |   |   |   |   |   |---feature_38 <= 0.50
|   |   |   |   |   |   |   |   |---feature_26 <= 4.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 4.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |   |   |   |---feature_26 >  4.50
|   |   |   |   |   |   |   |   |   |---feature_28 <= 3.00
|   |   |   |   |   |   |   |   |   |---feature_28 >  3.00
|   |   |   |   |   |   |   |---feature_38 >  0.50
|   |   |   |   |   |   |   |   |---feature_44 <= 8.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 5.00] -> 9
|   |   |   |   |   |   |   |   |---feature_44 >  8.50
|   |   |   |   |   |   |   |   |   |---feature_18 <= 12.00
|   |   |   |   |   |   |   |   |   |---feature_18 >  12.00
|   |   |   |   |   |---feature_27 >  7.50
|   |   |   |   |   |   |---feature_20 <= 15.50
|   |   |   |   |   |   |   |---feature_18 <= 2.50
|   |   |   |   |   |   |   |   |---feature_58 <= 4.00
|   |   |   |   |   |   |   |   |   |---feature_26 <= 1.00
|   |   |   |   |   |   |   |   |   |---feature_26 >  1.00
|   |   |   |   |   |   |   |   |---feature_58 >  4.00
|   |   |   |   |   |   |   |   |   |---feature_42 <= 14.00
|   |   |   |   |   |   |   |   |   |---feature_42 >  14.00
|   |   |   |   |   |   |   |---feature_18 >  2.50
|   |   |   |   |   |   |   |   |---feature_38 <= 3.50
|   |   |   |   |   |   |   |   |   |---feature_21 <= 4.00
|   |   |   |   |   |   |   |   |   |---feature_21 >  4.00
|   |   |   |   |   |   |   |   |---feature_38 >  3.50
|   |   |   |   |   |   |   |   |   |---feature_9 <= 2.50
|   |   |   |   |   |   |   |   |   |---feature_9 >  2.50
|   |   |   |   |   |   |---feature_20 >  15.50
|   |   |   |   |   |   |   |---feature_44 <= 6.50
|   |   |   |   |   |   |   |   |---feature_3 <= 13.50
|   |   |   |   |   |   |   |   |   |---feature_52 <= 9.50
|   |   |   |   |   |   |   |   |   |---feature_52 >  9.50
|   |   |   |   |   |   |   |   |---feature_3 >  13.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 17.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2
|   |   |   |   |   |   |   |---feature_44 >  6.50
|   |   |   |   |   |   |   |   |---feature_10 <= 13.50
|   |   |   |   |   |   |   |   |   |---feature_52 <= 9.00
|   |   |   |   |   |   |   |   |   |---feature_52 >  9.00
|   |   |   |   |   |   |   |   |---feature_10 >  13.50
|   |   |   |   |   |   |   |   |   |---feature_50 <= 7.50
|   |   |   |   |   |   |   |   |   |---feature_50 >  7.50
|   |   |   |---feature_33 >  3.50
|   |   |   |   |---feature_13 <= 9.50
|   |   |   |   |   |---feature_2 <= 1.50
|   |   |   |   |   |   |---feature_37 <= 3.50
|   |   |   |   |   |   |   |--- weights: [0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |   |   |   |---feature_37 >  3.50
|   |   |   |   |   |   |   |---feature_6 <= 8.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 105.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |   |   |   |   |---feature_6 >  8.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00] -> 9
|   |   |   |   |   |---feature_2 >  1.50
|   |   |   |   |   |   |---feature_46 <= 2.00
|   |   |   |   |   |   |   |--- weights: [0.00, 2.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |   |   |   |---feature_46 >  2.00
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00] -> 6
|   |   |   |   |---feature_13 >  9.50
|   |   |   |   |   |---feature_27 <= 14.50
|   |   |   |   |   |   |---feature_20 <= 4.50
|   |   |   |   |   |   |   |---feature_22 <= 2.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 2.00, 0.00, 0.00, 0.00] -> 6
|   |   |   |   |   |   |   |---feature_22 >  2.00
|   |   |   |   |   |   |   |   |---feature_18 <= 14.00
|   |   |   |   |   |   |   |   |   |---feature_38 <= 4.00
|   |   |   |   |   |   |   |   |   |---feature_38 >  4.00
|   |   |   |   |   |   |   |   |---feature_18 >  14.00
|   |   |   |   |   |   |   |   |   |--- weights: [2.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 0
|   |   |   |   |   |   |---feature_20 >  4.50
|   |   |   |   |   |   |   |---feature_42 <= 1.00
|   |   |   |   |   |   |   |   |---feature_11 <= 11.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 1.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
|   |   |   |   |   |   |   |   |---feature_11 >  11.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 1.00, 0.00, 0.00, 0.00, 0.00] -> 5
|   |   |   |   |   |   |   |---feature_42 >  1.00
|   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 5.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 4
|   |   |   |   |   |---feature_27 >  14.50
|   |   |   |   |   |   |---feature_12 <= 11.00
|   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 2.00, 0.00] -> 8
|   |   |   |   |   |   |---feature_12 >  11.00
|   |   |   |   |   |   |   |--- weights: [0.00, 17.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 1
Maybe it could be worth adding a parameter to optionally hide weights?

Also the above output has entries like,

|   |   |   |   |   |   |---feature_20 >  15.50
|   |   |   |   |   |   |   |---feature_44 <= 6.50
|   |   |   |   |   |   |   |   |---feature_3 <= 13.50
|   |   |   |   |   |   |   |   |   |---feature_52 <= 9.50
|   |   |   |   |   |   |   |   |   |---feature_52 >  9.50
|   |   |   |   |   |   |   |   |---feature_3 >  13.50
|   |   |   |   |   |   |   |   |   |--- weights: [0.00, 0.00, 17.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00, 0.00] -> 2

what happens if feature_3 <= 13.50 (and above conditions are verified) ? I'm not sure if it's an artefact of the classifier or of the tree representation. I guess the model will still predict a class in in this case?

A final point is that I think it might be worth making this as experimental as I don't think we want to keep backward compatibility on the output. This should be noted in the docstring and the readme if @jnothman agrees.

Please add a what's new entry.

Comment thread sklearn/tree/export.py Resolved
Comment thread sklearn/tree/export.py Resolved
Comment thread sklearn/tree/export.py Resolved
@JustGlowing

Copy link
Copy Markdown
Contributor Author

hi @rth thanks for your comments, here are my replies to your points:

  1. I agree that the output is unreadable when you have too many weights so I added the parameter hide_weights.
  2. Your confusion about some entries is due to the fact that the tree is truncated using the parameter max_depth=3 while the tree has 10 layers. To solve this issue I added the symbol ... when branches are truncated, the portion of tree you highlighted now looks like this:
|   |   |   |   |   |   |---feature_20 >  15.50
|   |   |   |   |   |   |   |---feature_44 <= 6.50
|   |   |   |   |   |   |   |   |---feature_3 <= 13.50
|   |   |   |   |   |   |   |   |   |---feature_52 <= 9.50 ...
|   |   |   |   |   |   |   |   |   |---feature_52 >  9.50 ...
|   |   |   |   |   |   |   |   |---feature_3 >  13.50
|   |   |   |   |   |   |   |   |   |----> 2
  1. I just updated the what's new.
Check here to see the full tree and snippet

from sklearn.tree import DecisionTreeClassifier 
from sklearn.tree import export_text 
from sklearn.datasets import load_digits 
 
digits = load_digits() 
 
X = digits.data 
y = digits.target 
 
clf = DecisionTreeClassifier(max_depth=10, random_state=0) 
clf.fit(X, y) 
print(export_text(clf, hide_weights=True))   

|---feature_36 <= 0.50
|   |---feature_28 <= 2.50
|   |   |---feature_21 <= 0.50
|   |   |   |---feature_6 <= 1.00
|   |   |   |   |---feature_29 <= 1.00
|   |   |   |   |   |---feature_44 <= 8.00
|   |   |   |   |   |   |---feature_37 <= 0.50
|   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |   |---feature_37 >  0.50
|   |   |   |   |   |   |   |----> 0
|   |   |   |   |   |---feature_44 >  8.00
|   |   |   |   |   |   |----> 6
|   |   |   |   |---feature_29 >  1.00
|   |   |   |   |   |----> 4
|   |   |   |---feature_6 >  1.00
|   |   |   |   |----> 5
|   |   |---feature_21 >  0.50
|   |   |   |---feature_18 <= 0.50
|   |   |   |   |----> 2
|   |   |   |---feature_18 >  0.50
|   |   |   |   |---feature_43 <= 14.50
|   |   |   |   |   |----> 0
|   |   |   |   |---feature_43 >  14.50
|   |   |   |   |   |----> 4
|   |---feature_28 >  2.50
|   |   |---feature_21 <= 6.50
|   |   |   |---feature_43 <= 0.50
|   |   |   |   |---feature_18 <= 8.50
|   |   |   |   |   |----> 9
|   |   |   |   |---feature_18 >  8.50
|   |   |   |   |   |----> 5
|   |   |   |---feature_43 >  0.50
|   |   |   |   |---feature_10 <= 11.50
|   |   |   |   |   |----> 6
|   |   |   |   |---feature_10 >  11.50
|   |   |   |   |   |----> 2
|   |   |---feature_21 >  6.50
|   |   |   |---feature_34 <= 7.50
|   |   |   |   |---feature_9 <= 10.50
|   |   |   |   |   |----> 9
|   |   |   |   |---feature_9 >  10.50
|   |   |   |   |   |----> 3
|   |   |   |---feature_34 >  7.50
|   |   |   |   |---feature_60 <= 11.00
|   |   |   |   |   |---feature_52 <= 10.50
|   |   |   |   |   |   |----> 8
|   |   |   |   |   |---feature_52 >  10.50
|   |   |   |   |   |   |----> 5
|   |   |   |   |---feature_60 >  11.00
|   |   |   |   |   |----> 0
|---feature_36 >  0.50
|   |---feature_21 <= 0.50
|   |   |---feature_42 <= 8.50
|   |   |   |---feature_5 <= 1.50
|   |   |   |   |---feature_19 <= 13.50
|   |   |   |   |   |---feature_37 <= 4.00
|   |   |   |   |   |   |---feature_58 <= 0.50
|   |   |   |   |   |   |   |---feature_26 <= 7.00
|   |   |   |   |   |   |   |   |----> 1
|   |   |   |   |   |   |   |---feature_26 >  7.00
|   |   |   |   |   |   |   |   |----> 8
|   |   |   |   |   |   |---feature_58 >  0.50
|   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |---feature_37 >  4.00
|   |   |   |   |   |   |---feature_43 <= 5.50
|   |   |   |   |   |   |   |---feature_26 <= 12.50
|   |   |   |   |   |   |   |   |----> 3
|   |   |   |   |   |   |   |---feature_26 >  12.50
|   |   |   |   |   |   |   |   |---feature_25 <= 3.00
|   |   |   |   |   |   |   |   |   |----> 9
|   |   |   |   |   |   |   |   |---feature_25 >  3.00
|   |   |   |   |   |   |   |   |   |----> 5
|   |   |   |   |   |   |---feature_43 >  5.50
|   |   |   |   |   |   |   |---feature_45 <= 6.50
|   |   |   |   |   |   |   |   |----> 7
|   |   |   |   |   |   |   |---feature_45 >  6.50
|   |   |   |   |   |   |   |   |---feature_46 <= 5.00
|   |   |   |   |   |   |   |   |   |----> 4
|   |   |   |   |   |   |   |   |---feature_46 >  5.00
|   |   |   |   |   |   |   |   |   |---feature_26 <= 10.50 ...
|   |   |   |   |   |   |   |   |   |---feature_26 >  10.50 ...
|   |   |   |   |---feature_19 >  13.50
|   |   |   |   |   |---feature_46 <= 0.50
|   |   |   |   |   |   |---feature_27 <= 10.50
|   |   |   |   |   |   |   |---feature_19 <= 15.00
|   |   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |   |   |---feature_19 >  15.00
|   |   |   |   |   |   |   |   |----> 4
|   |   |   |   |   |   |---feature_27 >  10.50
|   |   |   |   |   |   |   |----> 1
|   |   |   |   |   |---feature_46 >  0.50
|   |   |   |   |   |   |---feature_33 <= 4.00
|   |   |   |   |   |   |   |---feature_2 <= 2.00
|   |   |   |   |   |   |   |   |----> 6
|   |   |   |   |   |   |   |---feature_2 >  2.00
|   |   |   |   |   |   |   |   |---feature_1 <= 2.00
|   |   |   |   |   |   |   |   |   |----> 3
|   |   |   |   |   |   |   |   |---feature_1 >  2.00
|   |   |   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |   |---feature_33 >  4.00
|   |   |   |   |   |   |   |----> 4
|   |   |   |---feature_5 >  1.50
|   |   |   |   |---feature_18 <= 4.50
|   |   |   |   |   |---feature_10 <= 1.00
|   |   |   |   |   |   |----> 4
|   |   |   |   |   |---feature_10 >  1.00
|   |   |   |   |   |   |---feature_14 <= 5.50
|   |   |   |   |   |   |   |----> 3
|   |   |   |   |   |   |---feature_14 >  5.50
|   |   |   |   |   |   |   |----> 7
|   |   |   |   |---feature_18 >  4.50
|   |   |   |   |   |---feature_9 <= 15.00
|   |   |   |   |   |   |---feature_24 <= 0.50
|   |   |   |   |   |   |   |----> 5
|   |   |   |   |   |   |---feature_24 >  0.50
|   |   |   |   |   |   |   |----> 4
|   |   |   |   |   |---feature_9 >  15.00
|   |   |   |   |   |   |----> 8
|   |   |---feature_42 >  8.50
|   |   |   |---feature_54 <= 1.50
|   |   |   |   |---feature_10 <= 5.50
|   |   |   |   |   |---feature_41 <= 1.50
|   |   |   |   |   |   |---feature_18 <= 5.50
|   |   |   |   |   |   |   |----> 6
|   |   |   |   |   |   |---feature_18 >  5.50
|   |   |   |   |   |   |   |----> 1
|   |   |   |   |   |---feature_41 >  1.50
|   |   |   |   |   |   |----> 4
|   |   |   |   |---feature_10 >  5.50
|   |   |   |   |   |---feature_45 <= 1.50
|   |   |   |   |   |   |---feature_62 <= 1.50
|   |   |   |   |   |   |   |----> 1
|   |   |   |   |   |   |---feature_62 >  1.50
|   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |---feature_45 >  1.50
|   |   |   |   |   |   |---feature_50 <= 6.00
|   |   |   |   |   |   |   |---feature_5 <= 6.00
|   |   |   |   |   |   |   |   |----> 4
|   |   |   |   |   |   |   |---feature_5 >  6.00
|   |   |   |   |   |   |   |   |----> 5
|   |   |   |   |   |   |---feature_50 >  6.00
|   |   |   |   |   |   |   |---feature_38 <= 4.50
|   |   |   |   |   |   |   |   |----> 6
|   |   |   |   |   |   |   |---feature_38 >  4.50
|   |   |   |   |   |   |   |   |----> 7
|   |   |   |---feature_54 >  1.50
|   |   |   |   |---feature_58 <= 9.00
|   |   |   |   |   |----> 6
|   |   |   |   |---feature_58 >  9.00
|   |   |   |   |   |----> 2
|   |---feature_21 >  0.50
|   |   |---feature_60 <= 7.50
|   |   |   |---feature_5 <= 0.50
|   |   |   |   |---feature_12 <= 7.50
|   |   |   |   |   |----> 4
|   |   |   |   |---feature_12 >  7.50
|   |   |   |   |   |---feature_53 <= 10.50
|   |   |   |   |   |   |---feature_60 <= 1.50
|   |   |   |   |   |   |   |----> 7
|   |   |   |   |   |   |---feature_60 >  1.50
|   |   |   |   |   |   |   |---feature_26 <= 8.50
|   |   |   |   |   |   |   |   |----> 8
|   |   |   |   |   |   |   |---feature_26 >  8.50
|   |   |   |   |   |   |   |   |---feature_13 <= 7.50
|   |   |   |   |   |   |   |   |   |---feature_3 <= 14.50 ...
|   |   |   |   |   |   |   |   |   |---feature_3 >  14.50 ...
|   |   |   |   |   |   |   |   |---feature_13 >  7.50
|   |   |   |   |   |   |   |   |   |----> 5
|   |   |   |   |   |---feature_53 >  10.50
|   |   |   |   |   |   |---feature_61 <= 12.00
|   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |   |---feature_61 >  12.00
|   |   |   |   |   |   |   |----> 1
|   |   |   |---feature_5 >  0.50
|   |   |   |   |---feature_53 <= 3.50
|   |   |   |   |   |---feature_37 <= 0.50
|   |   |   |   |   |   |---feature_52 <= 1.00
|   |   |   |   |   |   |   |----> 7
|   |   |   |   |   |   |---feature_52 >  1.00
|   |   |   |   |   |   |   |---feature_20 <= 11.50
|   |   |   |   |   |   |   |   |---feature_52 <= 7.00
|   |   |   |   |   |   |   |   |   |----> 9
|   |   |   |   |   |   |   |   |---feature_52 >  7.00
|   |   |   |   |   |   |   |   |   |----> 8
|   |   |   |   |   |   |   |---feature_20 >  11.50
|   |   |   |   |   |   |   |   |---feature_1 <= 1.00
|   |   |   |   |   |   |   |   |   |---feature_11 <= 13.00 ...
|   |   |   |   |   |   |   |   |   |---feature_11 >  13.00 ...
|   |   |   |   |   |   |   |   |---feature_1 >  1.00
|   |   |   |   |   |   |   |   |   |----> 3
|   |   |   |   |   |---feature_37 >  0.50
|   |   |   |   |   |   |---feature_26 <= 12.50
|   |   |   |   |   |   |   |---feature_19 <= 12.50
|   |   |   |   |   |   |   |   |---feature_51 <= 2.50
|   |   |   |   |   |   |   |   |   |----> 3
|   |   |   |   |   |   |   |   |---feature_51 >  2.50
|   |   |   |   |   |   |   |   |   |----> 7
|   |   |   |   |   |   |   |---feature_19 >  12.50
|   |   |   |   |   |   |   |   |----> 8
|   |   |   |   |   |   |---feature_26 >  12.50
|   |   |   |   |   |   |   |---feature_27 <= 5.50
|   |   |   |   |   |   |   |   |----> 7
|   |   |   |   |   |   |   |---feature_27 >  5.50
|   |   |   |   |   |   |   |   |---feature_33 <= 1.50
|   |   |   |   |   |   |   |   |   |---feature_1 <= 3.00 ...
|   |   |   |   |   |   |   |   |   |---feature_1 >  3.00 ...
|   |   |   |   |   |   |   |   |---feature_33 >  1.50
|   |   |   |   |   |   |   |   |   |---feature_2 <= 6.50 ...
|   |   |   |   |   |   |   |   |   |---feature_2 >  6.50 ...
|   |   |   |   |---feature_53 >  3.50
|   |   |   |   |   |---feature_62 <= 0.50
|   |   |   |   |   |   |---feature_18 <= 1.00
|   |   |   |   |   |   |   |----> 3
|   |   |   |   |   |   |---feature_18 >  1.00
|   |   |   |   |   |   |   |---feature_44 <= 5.50
|   |   |   |   |   |   |   |   |----> 9
|   |   |   |   |   |   |   |---feature_44 >  5.50
|   |   |   |   |   |   |   |   |---feature_21 <= 12.00
|   |   |   |   |   |   |   |   |   |---feature_20 <= 9.50 ...
|   |   |   |   |   |   |   |   |   |---feature_20 >  9.50 ...
|   |   |   |   |   |   |   |   |---feature_21 >  12.00
|   |   |   |   |   |   |   |   |   |----> 8
|   |   |   |   |   |---feature_62 >  0.50
|   |   |   |   |   |   |----> 1
|   |   |---feature_60 >  7.50
|   |   |   |---feature_33 <= 3.50
|   |   |   |   |---feature_43 <= 1.50
|   |   |   |   |   |---feature_29 <= 12.50
|   |   |   |   |   |   |---feature_34 <= 3.50
|   |   |   |   |   |   |   |---feature_26 <= 8.50
|   |   |   |   |   |   |   |   |---feature_28 <= 7.50
|   |   |   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |   |   |   |---feature_28 >  7.50
|   |   |   |   |   |   |   |   |   |---feature_19 <= 14.50 ...
|   |   |   |   |   |   |   |   |   |---feature_19 >  14.50 ...
|   |   |   |   |   |   |   |---feature_26 >  8.50
|   |   |   |   |   |   |   |   |---feature_59 <= 13.50
|   |   |   |   |   |   |   |   |   |---feature_26 <= 10.00 ...
|   |   |   |   |   |   |   |   |   |---feature_26 >  10.00 ...
|   |   |   |   |   |   |   |   |---feature_59 >  13.50
|   |   |   |   |   |   |   |   |   |----> 5
|   |   |   |   |   |   |---feature_34 >  3.50
|   |   |   |   |   |   |   |---feature_42 <= 3.50
|   |   |   |   |   |   |   |   |---feature_25 <= 2.00
|   |   |   |   |   |   |   |   |   |---feature_2 <= 3.50 ...
|   |   |   |   |   |   |   |   |   |---feature_2 >  3.50 ...
|   |   |   |   |   |   |   |   |---feature_25 >  2.00
|   |   |   |   |   |   |   |   |   |----> 9
|   |   |   |   |   |   |   |---feature_42 >  3.50
|   |   |   |   |   |   |   |   |----> 8
|   |   |   |   |   |---feature_29 >  12.50
|   |   |   |   |   |   |---feature_35 <= 1.50
|   |   |   |   |   |   |   |---feature_3 <= 3.50
|   |   |   |   |   |   |   |   |----> 1
|   |   |   |   |   |   |   |---feature_3 >  3.50
|   |   |   |   |   |   |   |   |---feature_27 <= 7.50
|   |   |   |   |   |   |   |   |   |---feature_19 <= 3.50 ...
|   |   |   |   |   |   |   |   |   |---feature_19 >  3.50 ...
|   |   |   |   |   |   |   |   |---feature_27 >  7.50
|   |   |   |   |   |   |   |   |   |---feature_3 <= 15.50 ...
|   |   |   |   |   |   |   |   |   |---feature_3 >  15.50 ...
|   |   |   |   |   |   |---feature_35 >  1.50
|   |   |   |   |   |   |   |---feature_26 <= 2.50
|   |   |   |   |   |   |   |   |----> 3
|   |   |   |   |   |   |   |---feature_26 >  2.50
|   |   |   |   |   |   |   |   |---feature_21 <= 10.50
|   |   |   |   |   |   |   |   |   |---feature_51 <= 1.00 ...
|   |   |   |   |   |   |   |   |   |---feature_51 >  1.00 ...
|   |   |   |   |   |   |   |   |---feature_21 >  10.50
|   |   |   |   |   |   |   |   |   |---feature_11 <= 1.00 ...
|   |   |   |   |   |   |   |   |   |---feature_11 >  1.00 ...
|   |   |   |   |---feature_43 >  1.50
|   |   |   |   |   |---feature_27 <= 7.50
|   |   |   |   |   |   |---feature_34 <= 3.50
|   |   |   |   |   |   |   |---feature_50 <= 0.50
|   |   |   |   |   |   |   |   |---feature_12 <= 14.50
|   |   |   |   |   |   |   |   |   |---feature_14 <= 3.50 ...
|   |   |   |   |   |   |   |   |   |---feature_14 >  3.50 ...
|   |   |   |   |   |   |   |   |---feature_12 >  14.50
|   |   |   |   |   |   |   |   |   |----> 1
|   |   |   |   |   |   |   |---feature_50 >  0.50
|   |   |   |   |   |   |   |   |---feature_45 <= 14.00
|   |   |   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |   |   |   |---feature_45 >  14.00
|   |   |   |   |   |   |   |   |   |----> 3
|   |   |   |   |   |   |---feature_34 >  3.50
|   |   |   |   |   |   |   |---feature_38 <= 0.50
|   |   |   |   |   |   |   |   |---feature_26 <= 4.50
|   |   |   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |   |   |   |---feature_26 >  4.50
|   |   |   |   |   |   |   |   |   |---feature_28 <= 3.00 ...
|   |   |   |   |   |   |   |   |   |---feature_28 >  3.00 ...
|   |   |   |   |   |   |   |---feature_38 >  0.50
|   |   |   |   |   |   |   |   |---feature_44 <= 8.50
|   |   |   |   |   |   |   |   |   |----> 9
|   |   |   |   |   |   |   |   |---feature_44 >  8.50
|   |   |   |   |   |   |   |   |   |---feature_18 <= 12.00 ...
|   |   |   |   |   |   |   |   |   |---feature_18 >  12.00 ...
|   |   |   |   |   |---feature_27 >  7.50
|   |   |   |   |   |   |---feature_20 <= 15.50
|   |   |   |   |   |   |   |---feature_18 <= 2.50
|   |   |   |   |   |   |   |   |---feature_58 <= 4.00
|   |   |   |   |   |   |   |   |   |---feature_26 <= 1.00 ...
|   |   |   |   |   |   |   |   |   |---feature_26 >  1.00 ...
|   |   |   |   |   |   |   |   |---feature_58 >  4.00
|   |   |   |   |   |   |   |   |   |---feature_42 <= 14.00 ...
|   |   |   |   |   |   |   |   |   |---feature_42 >  14.00 ...
|   |   |   |   |   |   |   |---feature_18 >  2.50
|   |   |   |   |   |   |   |   |---feature_38 <= 3.50
|   |   |   |   |   |   |   |   |   |---feature_21 <= 4.00 ...
|   |   |   |   |   |   |   |   |   |---feature_21 >  4.00 ...
|   |   |   |   |   |   |   |   |---feature_38 >  3.50
|   |   |   |   |   |   |   |   |   |---feature_9 <= 2.50 ...
|   |   |   |   |   |   |   |   |   |---feature_9 >  2.50 ...
|   |   |   |   |   |   |---feature_20 >  15.50
|   |   |   |   |   |   |   |---feature_44 <= 6.50
|   |   |   |   |   |   |   |   |---feature_3 <= 13.50
|   |   |   |   |   |   |   |   |   |---feature_52 <= 9.50 ...
|   |   |   |   |   |   |   |   |   |---feature_52 >  9.50 ...
|   |   |   |   |   |   |   |   |---feature_3 >  13.50
|   |   |   |   |   |   |   |   |   |----> 2
|   |   |   |   |   |   |   |---feature_44 >  6.50
|   |   |   |   |   |   |   |   |---feature_10 <= 13.50
|   |   |   |   |   |   |   |   |   |---feature_52 <= 9.00 ...
|   |   |   |   |   |   |   |   |   |---feature_52 >  9.00 ...
|   |   |   |   |   |   |   |   |---feature_10 >  13.50
|   |   |   |   |   |   |   |   |   |---feature_50 <= 7.50 ...
|   |   |   |   |   |   |   |   |   |---feature_50 >  7.50 ...
|   |   |   |---feature_33 >  3.50
|   |   |   |   |---feature_13 <= 9.50
|   |   |   |   |   |---feature_2 <= 1.50
|   |   |   |   |   |   |---feature_37 <= 3.50
|   |   |   |   |   |   |   |----> 1
|   |   |   |   |   |   |---feature_37 >  3.50
|   |   |   |   |   |   |   |---feature_6 <= 8.00
|   |   |   |   |   |   |   |   |----> 4
|   |   |   |   |   |   |   |---feature_6 >  8.00
|   |   |   |   |   |   |   |   |----> 9
|   |   |   |   |   |---feature_2 >  1.50
|   |   |   |   |   |   |---feature_46 <= 2.00
|   |   |   |   |   |   |   |----> 1
|   |   |   |   |   |   |---feature_46 >  2.00
|   |   |   |   |   |   |   |----> 6
|   |   |   |   |---feature_13 >  9.50
|   |   |   |   |   |---feature_27 <= 14.50
|   |   |   |   |   |   |---feature_20 <= 4.50
|   |   |   |   |   |   |   |---feature_22 <= 2.00
|   |   |   |   |   |   |   |   |----> 6
|   |   |   |   |   |   |   |---feature_22 >  2.00
|   |   |   |   |   |   |   |   |---feature_18 <= 14.00
|   |   |   |   |   |   |   |   |   |---feature_38 <= 4.00 ...
|   |   |   |   |   |   |   |   |   |---feature_38 >  4.00 ...
|   |   |   |   |   |   |   |   |---feature_18 >  14.00
|   |   |   |   |   |   |   |   |   |----> 0
|   |   |   |   |   |   |---feature_20 >  4.50
|   |   |   |   |   |   |   |---feature_42 <= 1.00
|   |   |   |   |   |   |   |   |---feature_11 <= 11.50
|   |   |   |   |   |   |   |   |   |----> 1
|   |   |   |   |   |   |   |   |---feature_11 >  11.50
|   |   |   |   |   |   |   |   |   |----> 5
|   |   |   |   |   |   |   |---feature_42 >  1.00
|   |   |   |   |   |   |   |   |----> 4
|   |   |   |   |   |---feature_27 >  14.50
|   |   |   |   |   |   |---feature_12 <= 11.00
|   |   |   |   |   |   |   |----> 8
|   |   |   |   |   |   |---feature_12 >  11.00
|   |   |   |   |   |   |   |----> 1

@rth rth left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks @JustGlowing !

Let's wait for an opinion of another reviewer regarding whether we should put a note in the docs that the output of export_text may change without following the usual backward compatibility policy.

Comment thread doc/modules/tree.rst
>>> from sklearn.tree.export import export_text
>>> iris = load_iris()
>>> X = iris['data']
>>> y = iris['target']

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a sklearn.utils.Bunch object, so just iris.data, iris.target, iris.feature_names will work and might be more readable...

Also I guess one could directly use those in decision_tree.fit to save 2 lines..

@jnothman

Copy link
Copy Markdown
Member

Good work!

I'm happy for this to be noted as not having our usual backwards compatibility support.

Remaining minor comments:

  • Should we have show_weights=True or show_weights=False by default?
  • IIRC, you previously showed weights/values not only at the leaves. Should we still have that available as an option?
  • When show_weights=False, we get ----> virginica. Seems like a lot of -s.
  • When show_weights=True, we get | |--- weights: [50.00, 0.00, 0.00]-> setosa. Should there be a before the ->?
  • When truncated to a max_depth, you don't get to see information about the dominant class or mean value or complexity of the truncated tree. Should we make truncation more informative and explicit, e.g.:
    |---petal width (cm) >  0.80
    |   |---petal width (cm) <= 1.75 ...
    
    becomes
    |---petal width (cm) >  0.80
    |   |---petal width (cm) <= 1.75
    |   |   |--- 3 more layers hidden. weights = [...]
    
  • Truncation occurs even at the leaf. This seems to be a bit strange. Try the iris example with max_depth=2; two decisions are all that are used for iris here. Truncation, in summarising the remaining tree, should just print the leaf if that's what it finds. I know this seems to be a lot of extra logic for truncation... Tell me if you think I'm crazy.
  • Would it be good to add this (with a small max_depth) as a __str__ for Tree objects so that users can go print(dtc.tree_)??

@JustGlowing

Copy link
Copy Markdown
Contributor Author

hi @jnothman, I'll reply inline to your comments:

  • Should we have show_weights=True or show_weights=False by default?

I'd say show_weights=False, this way the user starts with a minimalistic result which is very readable.

  • IIRC, you previously showed weights/values not only at the leaves. Should we still have that available as an option?

I did, but I removed the option because I realized that 1) the interface of the function was quite complex 2) the output was unreadable. I'd suggest not to add them back.

  • When show_weights=False, we get ----> virginica. Seems like a lot of -s.
  • When show_weights=True, we get | |--- weights: [50.00, 0.00, 0.00]-> setosa. Should there be a before the ->?

I solved these two points replacing the -> with class:. Now the result seem more consistent. Example:

|---petal width (cm) <= 0.80
|   |--- weights: [50.00, 0.00, 0.00] class: setosa

Less hyphens and consistent notation as the markers now are weight:, class: and value:.

  • When truncated to a max_depth, you don't get to see information about the dominant class or mean value or complexity of the truncated tree. Should we make truncation more informative and explicit, e.g.:
    |---petal width (cm) >  0.80
    |   |---petal width (cm) <= 1.75 ...
    
    becomes
    |---petal width (cm) >  0.80
    |   |---petal width (cm) <= 1.75
    |   |   |--- 3 more layers hidden. weights = [...]
    
  • Truncation occurs even at the leaf. This seems to be a bit strange. Try the iris example with max_depth=2; two decisions are all that are used for iris here. Truncation, in summarising the remaining tree, should just print the leaf if that's what it finds. I know this seems to be a lot of extra logic for truncation... Tell me if you think I'm crazy.

Regarding these two points the confusion was due to the fact that export_text counted layers from 1 while the estimator counts from 0. I fixed them so that they match and added a bit of info when the layers are hidden.

Now if you train with max_depth=2 and use max_depth=2 to print the tree you get the full result including leaves:

decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
decision_tree.fit(X, y_str)
r = export_text(decision_tree, feature_names=iris['feature_names'], decimals=2, max_depth=2)

|---petal width (cm) <= 0.80
|   |--- weights: [50.00, 0.00, 0.00] class: setosa
|---petal width (cm) >  0.80
|   |---petal width (cm) <= 1.75
|   |   |--- weights: [0.00, 49.00, 5.00] class: versicolor
|   |---petal width (cm) >  1.75
|   |   |--- weights: [0.00, 1.00, 45.00] class: virginica

if you use max_depth=1 to print a tree that has max_depth=2 you get this:

|---petal width (cm) <= 0.80
|   |--- weights: [50.00, 0.00, 0.00] class: setosa
|---petal width (cm) >  0.80
|   |---petal width (cm) <= 1.75 ...1 layer/s hidden
|   |---petal width (cm) >  1.75 ...1 layer/s hidden

In this case the user exactly knows if a leaf has been hidden and can decide to tweak the parameter accordingly.

  • Would it be good to add this (with a small max_depth) as a __str__ for Tree objects so that users can go print(dtc.tree_)??

If there will be no backwards compatibility support I'd probably avoid this.

@jnothman

Copy link
Copy Markdown
Member

In this era of neural dominance, I wouldn't use "hidden layer"!

But I still think this is wrong:

In [2]: print(export_text(decision_tree, feature_names=iris['feature_names'], decimals=2, max_depth=1))
|---petal width (cm) <= 0.80
|   |--- class: setosa
|---petal width (cm) >  0.80
|   |---petal width (cm) <= 1.75 ...1 hidden layer/s
|   |---petal width (cm) >  1.75 ...1 hidden layer/s

I'd expect:

In [2]: print(export_text(decision_tree, feature_names=iris['feature_names'], decimals=2, max_depth=1))
|---petal width (cm) <= 0.80
|   |--- class: setosa
|---petal width (cm) >  0.80
|   |--- truncated branch with 2 leaves

or if you think this corresponds to max_depth=0, that's fine. Still, you shouldn't be showing a leaf's condition (e.g. "petal width (cm) <= 1.75") without its outcome value/class.

Also, now the space before "class" but not before feature name is a bit confusing visually.

@JustGlowing

Copy link
Copy Markdown
Contributor Author

good morning @jnothman !

"neural dominance" 😂 nice expression.

I see your point on not showing a leaf's condition without its outcome and I made some changes. Now the value of a leaf is always shown in case the condition is printed:

decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
decision_tree = decision_tree.fit(X, y)

r = export_text(decision_tree, feature_names=iris['feature_names'], max_depth=1)
print(r)
|--- petal width (cm) <= 0.80
|   |--- class: setosa
|--- petal width (cm) >  0.80
|   |--- petal width (cm) <= 1.75
|   |   |--- class: versicolor
|   |--- petal width (cm) >  1.75
|   |   |--- class: virginica

r = export_text(decision_tree, feature_names=iris['feature_names'], max_depth=0)
print(r)
|--- petal width (cm) <= 0.80
|   |--- class: setosa
|--- petal width (cm) >  0.80
|   |--- truncated branch of depth 2

The only difference with what you proposed is that with max_depth=0 the root of the tree is printed, I prefer this as it matches the way the estimator counts the layers.

I also added the space in front of the feature name.

@JustGlowing

Copy link
Copy Markdown
Contributor Author

hi @jnothman any thoughts?

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor things. Looking good!

Comment thread sklearn/tree/export.py Outdated
Number of decimal digits to display.

show_weights : bool, optional (default=False)
If true the classification weights will not be exported

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something is wrong here... true means not?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a misprint in the documentation. When show_weights is True, the weights are actually exported. Just fixed the misprint.

| |--- class: 1
""")
assert export_text(clf) == expected_report
assert export_text(clf, max_depth=0) == expected_report

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is a little surprising to read that you've grown the tree with max_depth=2 but are now asserting that it has depth 0..?

You can assert the max depth of the grown tree using clf.tree_.max_depth

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This tests that the leaves in the first level are not truncated. I just left a comment before the test.

Comment thread sklearn/tree/tests/test_export.py Outdated
clf = DecisionTreeClassifier(max_depth=2, random_state=0)
clf.fit(X, y)

expected_report = dedent("""|--- feature_1 <= 0.00

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why you're using dedent but then not indenting all the lines. I might have expected:

    expected_report = dedent("""
    |--- feature_1 <= 0.00
    |   |--- class: -1
    |--- feature_1 >  0.00
    |   |--- class: 1
    """).lstrip()

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree. Just modified accordingly.

Comment thread sklearn/tree/export.py
if feature_names:
feature_names_ = [feature_names[i] for i in tree_.feature]
else:
feature_names_ = ["feature_{}".format(i) for i in tree_.feature]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use ' ' instead of '_' I think

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or use x[i]??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that feature_27 is more readable than feature27 and that x[27] may be confusing as you may actually have a variable called x in your code.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant using a space rather than an underscore, not dropping the underscore

@jnothman jnothman left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great work! Thanks for bearing with my nitpicks!

@jnothman
jnothman merged commit a061ada into scikit-learn:master Feb 11, 2019
@rth

rth commented Feb 12, 2019

Copy link
Copy Markdown
Member

Great that this is merged! Thanks for your work @JustGlowing, and @jnothman for the final review!

@JustGlowing

Copy link
Copy Markdown
Contributor Author

Thanks to all the reviewers, it has been a great experience working with you!

xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
xhluca pushed a commit to xhluca/scikit-learn that referenced this pull request Apr 28, 2019
koenvandevelde pushed a commit to koenvandevelde/scikit-learn that referenced this pull request Jul 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants