gpt4 book ai didi

java - 分类时Weka nullPointerException

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:23:24 25 4
gpt4 key购买 nike

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

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com