Visualizing+a+Tree

toc The following code sample takes an ARFF file as input, trains a  and displays the generated tree with the  class.

This can be done with all classifiers that implement the interface.

= Source code = code format="java" import java.awt.BorderLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.io.BufferedReader; import java.io.FileReader; import javax.swing.JFrame; import weka.classifiers.trees.J48; import weka.core.Instances; import weka.gui.treevisualizer.PlaceNode2; import weka.gui.treevisualizer.TreeVisualizer; /** * Displays a trained J48 as tree. * Expects an ARFF filename as first argument. * * @author FracPete (fracpete at waikato dot ac dot nz) */ public class VisualizeJ48 { public static void main(String args[]) throws Exception { // train classifier J48 cls = new J48; Instances data = new Instances(new BufferedReader(new FileReader(args[0]))); data.setClassIndex(data.numAttributes - 1); cls.buildClassifier(data); // display classifier final javax.swing.JFrame jf = new javax.swing.JFrame("Weka Classifier Tree Visualizer: J48"); jf.setSize(500,400); jf.getContentPane.setLayout(new BorderLayout); TreeVisualizer tv = new TreeVisualizer(null,        cls.graph,         new PlaceNode2); jf.getContentPane.add(tv, BorderLayout.CENTER); jf.addWindowListener(new java.awt.event.WindowAdapter {      public void windowClosing(java.awt.event.WindowEvent e) {         jf.dispose;       }     }); jf.setVisible(true); tv.fitToScreen; } } code

= Downloads =
 * [[file:VisualizeJ48.java]] ([|book], [|stable-3.6], [|developer])