Tim Raynor Tim Raynor - 9 days ago 6
Java Question

weka java loading model and using test data set

I try to build weka models by doing the serialization and deserialization as the instruction says in the weka wikii. Using bayesnet build from training and want to load that model for test. Training and test have the same attribute
The setting of the Filter look like this:

Remove rm = generateFilter(filterOption);

FilteredClassifier fc = new FilteredClassifier();
fc.setFilter(rm);
filterClassifier.setClassifier(randomTree);
filterClassifier.buildClassifier(data);
exportClassifier("randomTree", file, filterClassifier);


The code for exporting is look like this:

private void exportClassifier(String method, String file,
FilteredClassifier filterClassifier) throws IOException,
FileNotFoundException {
System.out.println(file + "." + method + ".model");

ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(
file + "." + method + ".model"));
oos.writeObject(filterClassifier);
oos.flush();
oos.close();
}


but when I try to load them with another test set like:

public String EvaluateModel(String file, File modelFile) throws Exception {
Instances data = populateInstance(file);

if (data.classIndex() == -1) {
System.out.println("reset index...");
data.setClassIndex(data.numAttributes() - 1);
}

FilteredClassifier classifier = (FilteredClassifier) weka.core.SerializationHelper
.read(new FileInputStream(modelFile));

//classifier.buildClassifier(data);
Evaluation eval = new Evaluation(data);
//eval.crossValidateModel(classifier, data, 10, new Random(1));
eval.evaluateModel(classifier, data);

String summaryString = eval
.toSummaryString("\nResults\n======\n", false);

System.out.println(summaryString);
System.out.println(eval.fMeasure(1) + " " + eval.precision(1) + " "
+ eval.recall(1));
return formatOutput(eval);
}


I got exceptions like:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1200
at weka.classifiers.bayes.net.estimate.DiscreteEstimatorBayes.getProbability(DiscreteEstimatorBayes.java:106)
at weka.classifiers.bayes.net.estimate.SimpleEstimator.distributionForInstance(SimpleEstimator.java:183)
at weka.classifiers.bayes.BayesNet.distributionForInstance(BayesNet.java:386)
at weka.classifiers.meta.FilteredClassifier.distributionForInstance(FilteredClassifier.java:437)
at weka.classifiers.Evaluation.evaluateModelOnceAndRecordPrediction(Evaluation.java:1439)
at weka.classifiers.Evaluation.evaluateModel(Evaluation.java:1407)
at com.besmart.raynor.dataprocessing.dataprocessor.weka.WekaRunner.EvaluateModel(WekaRunner.java:138)
at com.besmart.raynor.dataprocessing.dataprocessor.weka.WekaBatchRunner.batchReEvaluation(WekaBatchRunner.java:80)
at com.besmart.raynor.dataprocessing.dataprocessor.weka.WekaBatchRunner.main(WekaBatchRunner.java:103)

Answer

Instead of writing the object using ObjectOutputStream, you can use weka.core.SerializationHelper.write method.