在weka java API上评估新数据时出错

时间:2014-11-18 08:03:27

标签: java nullpointerexception weka

当我试图评估已经训练过的分类器的新测试数据时,它会给我带来零点异常。

这是代码

String[] optionsj = new String[2];

optionsj[0] = "-C";
optionsj[1] = "0.25";

// a decision tree
//Training
Classifier cls = new J48();
cls.setOptions(optionsj);
Evaluation eval = new Evaluation(data);
Random rand = new Random(1);  // using seed = 1
int folds = 10;
eval.crossValidateModel(cls, data, folds, rand);
System.out.println(eval.toSummaryString());     

System.out.println(eval.toClassDetailsString());

//Testing

Evaluation evaltest = new Evaluation(data);
evaltest.evaluateModel(cls, data2);  // line 193
System.out.println(evaltest.toSummaryString());

//data and data2 are two instances of same dataset. 

我得到的错误是

Exception in thread "main" java.lang.NullPointerExceptionat 
at weka.classifiers.trees.J48.distributionForInstance(Unknown Source)
at weka.classifiers.Evaluation.evaluateModelOnceAndRecordPrediction(Unknown Source)
at weka.classifiers.Evaluation.evaluateModel(Unknown Source)
at dentis.Simplified.main(Simplified.java:193)

我无法打印数据集而没有任何问题。这是什么问题?为什么会发生以及如何解决它?

1 个答案:

答案 0 :(得分:2)

当您使用evaluateModel时,您需要首先在训练集上构建分类器。

cls.buildClassifier(data);