3
我正在使用該模型來訓練模型和再次分類。Weka nullPointerException分類時
我正確地獲取第一部分的統計數據,但不是第二部分。 它在再次評估時給出nullPointerException。我已經嘗試了所有類型的操作像測試它在代碼中創建一個實例等
java.lang.NullPointerException
at weka.classifiers.trees.m5.M5Base.classifyInstance(M5Base.java:514)
at wekaTest.<init>(wekaTest.java:44)
at wekaTest.main(wekaTest.java:71)
我寫的代碼片段是:
wekaTest()
{
try
{
FileReader reader = new FileReader("3.arff");
Instances instances = new Instances(reader);
// Make the last attribute be the class
int numAttr = instances.numAttributes();
instances.setClassIndex(numAttr - 1);
M5P tree = new M5P();
Evaluation eval = new Evaluation(instances);
eval.crossValidateModel(tree, instances, 10, new Random(1));
System.out.println(eval.toSummaryString("\nResults\n======\n", false));
weka.core.SerializationHelper.write("/path/tree.model", tree);
reader.close();
FileReader reader2 = new FileReader("3.arff");
Instances instances2 = new Instances(reader2);
instances2.setClassIndex(instances2.numAttributes() - 1);
reader2.close();
Instances labeled = new Instances(instances2);
Classifier cls = (Classifier) weka.core.SerializationHelper.read("/path/tree.model");
//instances2.deleteAttributeAt(numAttr-1);
for(int j=0; j<instances2.numInstances() ;j++)
{
//instance temp = new instance(instances2.instance(j));
//instances2.instance(j).setValue(numAttr-1,-1);
System.out.println("The instance: " + instances2.instance(j));
double clsLabel = tree.classifyInstance(instances2.instance(j));
labeled.instance(j).setClassValue(clsLabel);
}
}
catch(Exception ex) { ex.printStackTrace(); }
}