@@ -80,14 +80,18 @@ length(ensemble::Ensemble) = length(ensemble.trees)
8080depth (leaf:: Leaf ) = 0
8181depth (tree:: Node ) = 1 + max (depth (tree. left), depth (tree. right))
8282
83+ function print_tree (io:: IO , leaf:: Leaf , depth= - 1 , indent= 0 ; feature_names= nothing )
84+ n_matches = count (leaf. values .== leaf. majority)
85+ ratio = string (n_matches, " /" , length (leaf. values))
86+ println (io, " $(leaf. majority) : $(ratio) " )
87+ end
8388function print_tree (leaf:: Leaf , depth= - 1 , indent= 0 ; feature_names= nothing )
84- matches = findall (leaf. values .== leaf. majority)
85- ratio = string (length (matches)) * " /" * string (length (leaf. values))
86- println (" $(leaf. majority) : $(ratio) " )
89+ return print_tree (stdout , leaf, depth, indent; feature_names= feature_names)
8790end
8891
92+
8993"""
90- print_tree(tree::Node, depth=-1, indent=0; feature_names=nothing)
94+ print_tree([io::IO,] tree::Node, depth=-1, indent=0; sigdigits=4, feature_names=nothing)
9195
9296Print a textual visualization of the specified `tree`. For example, if
9397for some input pattern the value of "Feature 3" is "-30" and the value
@@ -100,7 +104,7 @@ non-majority classes are not shown.
100104
101105# Example output:
102106```
103- Feature 3 < -28.15
107+ Feature 3 < -28.15 ?
104108├─ Feature 2 < -161.0 ?
105109 ├─ 5 : 842/3650
106110 └─ 7 : 2493/10555
@@ -113,20 +117,24 @@ To facilitate visualisation of trees using third party packages, a `DecisionTree
113117`DecisionTree.Node` object can be wrapped to obtain a tree structure implementing the
114118AbstractTrees.jl interface. See [`wrap`](@ref)` for details.
115119"""
116- function print_tree (tree:: Node , depth= - 1 , indent= 0 ; feature_names= nothing )
120+ function print_tree (io :: IO , tree:: Node , depth= - 1 , indent= 0 ; sigdigits = 2 , feature_names= nothing )
117121 if depth == indent
118- println ()
122+ println (io )
119123 return
120124 end
125+ featval = round (tree. featval; sigdigits= sigdigits)
121126 if feature_names === nothing
122- println (" Feature $(tree. featid) < $(tree . featval) " )
127+ println (io, " Feature $(tree. featid) < $featval ? " )
123128 else
124- println (" Feature $(tree. featid) : \" $(feature_names[tree. featid]) \" < $(tree . featval) " )
129+ println (io, " Feature $(tree. featid) : \" $(feature_names[tree. featid]) \" < $featval ? " )
125130 end
126- print (" " ^ indent * " ├─ " )
127- print_tree (tree. left, depth, indent + 1 ; feature_names = feature_names)
128- print (" " ^ indent * " └─ " )
129- print_tree (tree. right, depth, indent + 1 ; feature_names = feature_names)
131+ print (io, " " ^ indent * " ├─ " )
132+ print_tree (io, tree. left, depth, indent + 1 ; feature_names= feature_names)
133+ print (io, " " ^ indent * " └─ " )
134+ print_tree (io, tree. right, depth, indent + 1 ; feature_names= feature_names)
135+ end
136+ function print_tree (tree:: Node , depth= - 1 , indent= 0 ; sigdigits= 2 , feature_names= nothing )
137+ return print_tree (stdout , tree, depth, indent; sigdigits= sigdigits, feature_names= feature_names)
130138end
131139
132140function show (io:: IO , leaf:: Leaf )
0 commit comments