Repository navigation
[MRG] Print Decision Trees in ASCII format - #9424
Conversation
|
I think that there is a pure matplotlib PR. Not sure what is the best |
|
@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. |
|
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. |
|
I also think it's a good idea to have an ascii version.
|
|
There's a PR to only use matplotlib and not dot and graphviz: #9251 |
|
The other methods have a maximum depth parameter to plot only the top of large trees. |
|
We probably don't want this as a method for now, right? It should be a function, I think. |
|
+1 for plain function from plotting module
|
|
It should follow the format of the other inspection techniques, so a function might be more appropriate. |
|
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 and a small spacing for a large one: One can also decide not to print value and class for each to get a more compact result: |
|
I'll write a comment describing the last changes in the following days. Sorry for closing the PR, I hit the the wrong button. |
|
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:
I also created this script to check the output in two specific cases with reasonable parameters: Here's the result: 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 ;-) |
There was a problem hiding this comment.
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] -> 1Also 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.
|
hi @rth thanks for your comments, here are my replies to your points:
Check here to see the full tree and snippet
|
rth
left a comment
There was a problem hiding this comment.
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.
| >>> from sklearn.tree.export import export_text | ||
| >>> iris = load_iris() | ||
| >>> X = iris['data'] | ||
| >>> y = iris['target'] |
There was a problem hiding this comment.
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..
|
Good work! I'm happy for this to be noted as not having our usual backwards compatibility support. Remaining minor comments:
|
|
hi @jnothman, I'll reply inline to your comments:
I'd say
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.
I solved these two points replacing the Less hyphens and consistent notation as the markers now are
Regarding these two points the confusion was due to the fact that Now if you train with if you use In this case the user exactly knows if a leaf has been hidden and can decide to tweak the parameter accordingly.
If there will be no backwards compatibility support I'd probably avoid this. |
|
In this era of neural dominance, I wouldn't use "hidden layer"! But I still think this is wrong: I'd expect: 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. |
|
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: The only difference with what you proposed is that with I also added the space in front of the feature name. |
|
hi @jnothman any thoughts? |
jnothman
left a comment
There was a problem hiding this comment.
Very minor things. Looking good!
| Number of decimal digits to display. | ||
|
|
||
| show_weights : bool, optional (default=False) | ||
| If true the classification weights will not be exported |
There was a problem hiding this comment.
Something is wrong here... true means not?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
This tests that the leaves in the first level are not truncated. I just left a comment before the test.
| clf = DecisionTreeClassifier(max_depth=2, random_state=0) | ||
| clf.fit(X, y) | ||
|
|
||
| expected_report = dedent("""|--- feature_1 <= 0.00 |
There was a problem hiding this comment.
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()There was a problem hiding this comment.
Agree. Just modified accordingly.
| if feature_names: | ||
| feature_names_ = [feature_names[i] for i in tree_.feature] | ||
| else: | ||
| feature_names_ = ["feature_{}".format(i) for i in tree_.feature] |
There was a problem hiding this comment.
use ' ' instead of '_' I think
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Sorry, I meant using a space rather than an underscore, not dropping the underscore
jnothman
left a comment
There was a problem hiding this comment.
Great work! Thanks for bearing with my nitpicks!
|
Great that this is merged! Thanks for your work @JustGlowing, and @jnothman for the final review! |
|
Thanks to all the reviewers, it has been a great experience working with you! |
This reverts commit d74922c.
This reverts commit d74922c.
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:
Notes: