0
我嘗試通過執行序列化和反序列化來構建weka模型,如weka wikii中的說明所述。使用培訓中的bayesnet構建並希望加載該模型進行測試。培訓和測試具有相同的屬性 過濾器的設置是這樣的:weka java加載模型和使用測試數據集
Remove rm = generateFilter(filterOption);
FilteredClassifier fc = new FilteredClassifier();
fc.setFilter(rm);
filterClassifier.setClassifier(randomTree);
filterClassifier.buildClassifier(data);
exportClassifier("randomTree", file, filterClassifier);
導出的代碼是這個樣子:
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();
}
但是當我嘗試另一個測試加載它們設置這樣的:
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);
}
我喜歡例外:
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)