After deep Google searches (and because the documentation provides minimal help) I finally found the answer.
I hope this explicit answer helps others in the future.
For a sample code I saw the question "How to print out the predicted class after cross-validation in WEKA" and I'm glad I was able to decode the incomplete answer wherein some of it is hard to understand.
Here is my code that worked similar to the GUI's output
StringBuffer predictionSB = new StringBuffer(); Range attributesToShow = null; Boolean outputDistributions = new Boolean(true); PlainText predictionOutput = new PlainText(); predictionOutput.setBuffer(predictionSB); predictionOutput.setOutputDistribution(true); Evaluation evaluation = new Evaluation(data); evaluation.crossValidateModel(j48Model, data, numberOfFolds, randomNumber, predictionOutput, attributesToShow, outputDistributions);
To help you understand, we need to implement the
StringBuffer
to be casted in anAbstractOutput
object so that the functioncrossValidateModel
can recognize it.Using
StringBuffer
only will cause ajava.lang.ClassCastException
similar the one in the question while using aPlainText
without aStringBuffer
will show ajava.lang.IllegalStateException
.I would like to thank ManChon U (Kevin) and their question "How to identify the cross-evaluation result to its corresponding instance in the input data set?" for giving me a clue on what this meant:
... you just need a single addition argument that is a concrete subclass of
weka.classifiers.evaluation.output.prediction.AbstractOutput
.weka.classifiers.evaluation.output.prediction.PlainText
is probably the one you want to use. Sourceand
... Try creating a
PlainText
object, which extendsAbstractOutput
(called output for example) instance and callingoutput.setBuffer(forPredictionsPrinting)
and passing that in instead of the buffer. SourceThese just actually meant to create a
PlainText
object, put aStringBuffer
in it and use it to tweak the output with methodssetOutput(boolean)
and others.Finally, to get our desired predictions, just use:
System.out.println(predictionOutput.getBuffer());
Wherein
predictionOutput
is an object from theAbstractOutput
family (PlainText
,CSV
,XML
, etc).Additionally, the results of
evaluation.predictions()
is different from the one provided in the WEKA GUI. Fortunately Mark Hall explained this in the question "Print out the predict class after cross-validation"Evaluation.predictions()
returns aFastVector
containing eitherNominalPrediction
orNumericPrediction
objects from theweka.classifiers.evaluation
package. CallingEvaluation.crossValidateModel()
with the additionalAbstractOutput
object results in the evaluation object printing the prediction/distribution information fromNominal
/NumericPrediction
objects to theStringBuffer
in the format that you see in the Explorer or from the command line.
References: