Kevin Kevin - 5 months ago 59x
Java Question

How to print out the predicted class after cross-validation in WEKA

Once a 10-fold cross-validation is done with a classifier, how can I print out the prediced class of every instance and the distribution of these instances?

J48 j48 = new J48();
Evaluation eval = new Evaluation(newData);
eval.crossValidateModel(j48, newData, 10, new Random(1));

When I tried something similar to below, it said that the classifier is not built.

for (int i=0; i<data.numInstances(); i++){

What I'm trying to do is the same function as in the WEKA GUI wherein once a classifier is trained, I can click on
Visualize classifier error" > Save
, and I will find the predicted class in the file. But now I need it in to work in my own Java code.

I have tried something like below:

J48 j48 = new J48();
Evaluation eval = new Evaluation(newData);
StringBuffer forPredictionsPrinting = new StringBuffer();
weka.core.Range attsToOutput = null;
Boolean outputDistribution = new Boolean(true);
eval.crossValidateModel(j48, newData, 10, new Random(1), forPredictionsPrinting, attsToOutput, outputDistribution);

Yet it prompts me the error:

Exception in thread "main" java.lang.ClassCastException: java.lang.StringBuffer cannot be cast to weka.classifiers.evaluation.output.prediction.AbstractOutput


The crossValidateModel() method can take a forPredictionsPrinting varargs parameter that is a weka.classifiers.evaluation.output.prediction.AbstractOutput instance.

The important part of that is a StringBuffer to hold a string representation of all the predictions. The following code is in untested JRuby, but you should be able to convert it for your needs.

j48 =
eval =
predictions =
eval.crossValidateModel(j48, newData, 10,, predictions,'1'), true)
# variable predictions now hold a string of all the individual predictions