Skip to content

Commit

Permalink
Changed DecisionTreeToRules to produce unique leafs. This makes the d…
Browse files Browse the repository at this point in the history
…ecision tree visualization clearer.
  • Loading branch information
antononcube committed Jul 17, 2013
1 parent 702b144 commit 048e6b7
Showing 1 changed file with 41 additions and 29 deletions.
70 changes: 41 additions & 29 deletions AVCDecisionTreeForest.m
Original file line number Diff line number Diff line change
Expand Up @@ -435,37 +435,49 @@ Mathematica is (C) Copyright 1988-2012 Wolfram Research, Inc.

(* This function transforms a decision tree into rules to be given to GraphPlot and related functions. *)

Clear[MakeIDGenerator]
MakeIDGenerator[] :=
Module[{i = 0},
Clear[NewID, ReetID];
NewID[] := i++;
ResetID[] := (i = 0);
];

Clear[TreeToRulesRecStep, LeafNodeDecoration]
LeafNodeDecoration[node_] := If[MatrixQ[node], Column[{Row[{"leaf ", NewID[]}], Grid[node]}], node];
TreeToRulesRecStep[tree_] :=
Which[
tree === {}, {},
Rest[tree] === {}, {},
MatrixQ[tree] && Dimensions[tree][[2]] == 2, {},
Length[Rest[tree]] == 2,
Join[
MapThread[
Which[
#2 == "True" && Length[tree[[1]]] >= 4 && (tree[[1, 4]] === Number || tree[[1, 4]] === Dot),
{tree[[1]] -> LeafNodeDecoration[#1[[1]]], "\[LessEqual] " <> ToString[NumberForm[tree[[1, 2]], 2]]},
#2 == "False" && Length[tree[[1]]] >= 4 && (tree[[1, 4]] === Number || tree[[1, 4]] === Dot),
{tree[[1]] -> LeafNodeDecoration[#1[[1]]], "> " <> ToString[NumberForm[tree[[1, 2]], 2]]},
#2 == "True" && Length[tree[[1]]] >= 4 && tree[[1, 4]] === Symbol,
{tree[[1]] -> LeafNodeDecoration[#1[[1]]], "= " <> ToString[tree[[1, 2]]]},
#2 == "False" && Length[tree[[1]]] >= 4 && tree[[1, 4]] === Symbol,
{tree[[1]] -> LeafNodeDecoration[#1[[1]]], "\[NotEqual] " <> ToString[tree[[1, 2]]]},
True,
{tree[[1]] -> LeafNodeDecoration[#1[[1]]], #2}
] &, {Rest[tree], {"True", "False"}}, 1],
Flatten[TreeToRulesRecStep[#] & /@ Rest[tree], 1]
],
True,
Join[Map[{tree[[1]] -> LeafNodeDecoration[#1[[1]]]} &, Rest[tree], {1}], Flatten[TreeToRulesRecStep[#] & /@ Rest[tree], 1]]
];

Clear[DecisionTreeToRules]
DecisionTreeToRules[tree_] :=
Which[
tree === {}, {},
Rest[tree] === {}, {},
MatrixQ[tree] && Dimensions[tree][[2]] == 2, {},
Length[Rest[tree]] == 2,
Join[
MapThread[
Which[
#2 == "True" && Length[tree[[1]]] >= 4 &&
(tree[[1, 4]] === Number || tree[[1, 4]] === Dot), {tree[[1]] -> #1[[1]],
"\[LessEqual] " <> ToString[NumberForm[tree[[1, 2]], 2]]},
#2 == "False" && Length[tree[[1]]] >= 4 &&
(tree[[1, 4]] === Number || tree[[1, 4]] === Dot), {tree[[1]] -> #1[[1]],
"> " <> ToString[NumberForm[tree[[1, 2]], 2]]},
#2 == "True" && Length[tree[[1]]] >= 4 &&
tree[[1, 4]] === Symbol, {tree[[1]] -> #1[[1]],
"= " <> ToString[tree[[1, 2]]]},
#2 == "False" && Length[tree[[1]]] >= 4 &&
tree[[1, 4]] === Symbol, {tree[[1]] -> #1[[1]],
"\[NotEqual] " <> ToString[tree[[1, 2]]]},
True, {tree[[1]] -> #1[[1]], #2}
] &, {Rest[tree], {"True", "False"}}, 1],
Flatten[DecisionTreeToRules[#] & /@ Rest[tree], 1]
],
True,
Join[Map[{tree[[1]] -> #[[1]]} &, Rest[tree], {1}],
Flatten[DecisionTreeToRules[#] & /@ Rest[tree], 1]]
] /. {{r_Rule, edge_String} :> {r,
Style[edge, Background -> White, FontSlant -> Plain]}};
Block[{},
MakeIDGenerator[];
TreeToRulesRecStep[tree]
] /. {{r_Rule, edge_String} :> {r, Style[StandardForm[edge], Background -> White, FontSlant -> Plain]}};


(* Centralize data *)

Expand Down

0 comments on commit 048e6b7

Please sign in to comment.