Skip to content

Commit

Permalink
Add simple decision tree visualizer
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Jul 20, 2015
1 parent 156b536 commit 79c9553
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@
be accompanied by a bump in version number, regardless of how minor the change.
-->

<version>0.9.3</version>

<version>0.9.4</version>

<repositories>
<repository>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package quickml.supervised.tree.decisionTree;

import quickml.supervised.tree.decisionTree.valueCounters.ClassificationCounter;
import quickml.supervised.tree.nodes.Branch;
import quickml.supervised.tree.nodes.Leaf;
import quickml.supervised.tree.nodes.Node;

import java.io.PrintStream;

/**
* Created by ian on 7/20/15.
*/
public class DecisionTreeVisualizer {

public static final int INDENT_AMOUNT = 3;

public void visualize(DecisionTree tree, PrintStream out) {
visualize(tree.root, out, 0);
}

private void visualize(final Node<ClassificationCounter> node, final PrintStream out, final int depth) {
StringBuilder indentBuilder = new StringBuilder();
for (int x = 0; x < depth; x++) {
indentBuilder.append(' ');
}
String indent = indentBuilder.toString();

if (node instanceof Branch) {
Branch<ClassificationCounter> branch = (Branch<ClassificationCounter>) node;
out.println(indent + branch.toString() + " TRUE:");
visualize(branch.getTrueChild(), out, depth + INDENT_AMOUNT);
out.println(indent + branch.toString() + " FALSE:");
visualize(branch.getFalseChild(), out, depth + INDENT_AMOUNT);
} else if (node instanceof Leaf) {
out.println(indent + "LEAF: " + node.toString());
}
}

}

0 comments on commit 79c9553

Please sign in to comment.