user1887967 user1887967 - 1 year ago 210
Java Question

How can I use weka to implement a decision tree?

I'm working with java, eclipse and weka, I want to show the tree with every rule and the predictin of a set of data to test my decision tree.

I was trying somenthing with this code but it's not doing what I need which is to show all the tree with every possible rule. I can only see part of the tree not all.

And I haven't been able to test it with a data test and data training, I think it has somenthing to do with the format I'm using in the text and train file.

So the question is, how can I show the tree with every possible decition and then test it ??

This is what I have so far:

public class Test {
public static BufferedReader readDataFile(String filename) {
BufferedReader inputReader = null;

try {
inputReader = new BufferedReader(new FileReader(filename));
} catch (FileNotFoundException ex) {
System.err.println("File not found: " + filename);

return inputReader;

public static void main(String[] args) throws Exception {

//Get File
BufferedReader reader = readDataFile("maitre.txt");

//Get the data
Instances data = new Instances(reader);

//Setting class attribute
data.setClassIndex(data.numAttributes() - 1);

//Make tree
J48 tree = new J48();
String[] options = new String[1];
options[0] = "-U";

//Print tree

//Predictions with test and training set of data

BufferedReader datafile = readDataFile("maitre.txt");
BufferedReader testfile = readDataFile("maitretest.txt");

Instances train = new Instances(datafile);
data.setClassIndex(data.numAttributes() - 1); // from somewhere
Instances test = new Instances(testfile);
data.setClassIndex(data.numAttributes() - 1); // from somewhere
// train classifier
Classifier cls = new J48();
// evaluate classifier and print some statistics
Evaluation eval = new Evaluation(train);
eval.evaluateModel(cls, test);
System.out.println(eval.toSummaryString("\nResults\n======\n", false));

the error:

Exception in thread "main" weka.core.UnassignedClassException: weka.classifiers.trees.j48.C45PruneableClassifierTree: Class attribute not set!
at weka.core.Capabilities.test(
at weka.core.Capabilities.test(
at weka.core.Capabilities.testWithFail(
at weka.classifiers.trees.j48.C45PruneableClassifierTree.buildClassifier(
at weka.classifiers.trees.J48.buildClassifier(
at Test.main(

maitre.txt and maitretest.txt have are somenthing like this:

@relation maitre

@attribute patrons {none, some, full}
@attribute waitEstation {0-10,10-30,30-60,>60}
@attribute reservation {TRUE, FALSE}
@attribute bar {TRUE, FALSE}
@attribute alternative {TRUE, FALSE}
@attribute sit {yes, no}


Answer Source

Yes ofcourse it has problem with your file format. save the both files as .arff file and then repeat the process. And yes for that you will need to read arff files with arffloader....below code will help you...

        ArffLoader loader= new ArffLoader();
        loader.setSource(new File("C:/Users/..../Desktop/maitre.arff"));
        Instances data= loader.getDataSet();

Good luck :)