London guy
London guy

Reputation: 28012

WEKA Instance.ClassAttribute() method

I WEKA, I came across the following code:

Instances train = DataSource.read(args[0]);
train.setClassIndex(train.numAttributes() - 1);
Instances test = DataSource.read(args[1]);
test.setClassIndex(test.numAttributes() - 1);
// train classifier
J48 cls = new J48();
cls.buildClassifier(train);
// output predictions
System.out.println("# - actual - predicted - distribution");
for (int i = 0; i < test.numInstances(); i++) {
    double pred = cls.classifyInstance(test.instance(i));
    double[] dist = cls.distributionForInstance(test.instance(i));
    System.out.print((i+1) + " - ");
    System.out.print(test.instance(i).toString(test.classIndex()) + " - ");
    System.out.print(test.classAttribute().value((int) pred) + " - ");
    System.out.println(Utils.arrayToString(dist));
}

The objective here is to run a pre-built classifier on a test set of data, and then to print the actual class, predicted class and the class membership value distribution. I understand everything except one line:

System.out.print(test.classAttribute().value((int) pred) + " - ");

If "test" is a group of instances, how will the above statement be able to print the predicted class value for the current instance inside the for loop?

Thanks Abhishek S

Upvotes: 1

Views: 518

Answers (1)

Sicco
Sicco

Reputation: 6271

I think that test.classAttribute() gives you all the classes that the test instances could be assigned to. The second part .value((int) pred) then selects the class out of this group which matches to pred, which is the predicted class for the current test instance.

Upvotes: 1

Related Questions