I am using for training the model and classifying again by using the model.
I am correctly getting the statistics for the first part but not the second part. It gives nullPointerException while evaluating it again. I have tried all sort of manipulations like testing it on one instance created within code etc.
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)
The code snippet I have written is:
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(); }
}
May be the tree you are writing is not yet initialized.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With