java - 分类时Weka nullPointerException

标签 java nullpointerexception machine-learning weka linear-regression

我正在使用它来训练模型并再次使用该模型进行分类。

我正确地获得了第一部分的统计数据,但没有获得第二部分的统计数据。 它在再次评估时给出 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(); }
}

最佳答案

可能是你写的树还没有初始化

关于java - 分类时Weka nullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9256485/

相关文章:

java - 多线程通过列表正确

java - 无法处理与 jSTL 相关的 Jasper 验证错误

java - 实例变量为 null 时抛出的异常

python - 为什么 TensorFlow 估计器无法进行这种简单的线性回归预测

machine-learning - 液态状态机: How it works and how to use it?

JavaFX 获取我的应用程序的 PrimaryStage(主菜单)

java - 如何在 Springboot 和 Spring Security 中身份验证成功后授权 Rest API 调用

java - NullPointerException:在不期望的地方抛出

java - 使用.getWidth()时无法定位空指针的原因

machine-learning - 使用 libsvm 提高标准化准确性的建议