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(); } 
} 

回答

1

謝謝Aditya。其實,你是對的!當我在10次交叉驗證後寫入時,變量尚未初始化。

片段如下:

try 
    { 
     FileReader reader2 = new FileReader("3.arff"); 
     Instances instances2 = new Instances(reader2); 
     instances2.setClassIndex(instances2.numAttributes() - 1); 
     reader2.close(); 
     int numAttr = instances2.numAttributes(); 

     Instances labeled = new Instances(instances2); 
     Classifier cls = (Classifier) weka.core.SerializationHelper.read("/home/sumit/Desktop/weka test/tree.model"); 
     cls.setDebug(true); 

     Instance inst = new Instance(4); 
     inst.setValue(0, instances2.instance(0).value(0)); 
     inst.setValue(1, instances2.instance(0).value(1)); 
     inst.setValue(2, instances2.instance(0).value(2)); 
     inst.setValue(3, -1); 
     double clsLabelTest = cls.classifyInstance(inst); 
     System.out.println(clsLabelTest); 

     //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 = cls.classifyInstance(instances2.instance(j)); 
       labeled.instance(j).setClassValue(clsLabel); 
     } 
     BufferedWriter writer = new BufferedWriter(new FileWriter("/home/sumit/Desktop/weka test/labeled.arff"));   
     writer.write(labeled.toString()); 
     writer.newLine(); 
     writer.flush(); 
     writer.close(); 
     // Test the model 
     //Evaluation eTest = new Evaluation(instances2); 
     //eTest.evaluateModel(cls, instances2); 
    } 
3

可能是你正在寫的樹還沒有初始化。