我正在进行特征选择,我想使用决策树(DT)分类器作为结果的评估者。我遇到的问题是,DT无法用名义值对测试实例进行分类。这会使代码引发以下异常
Exception in thread "main" net.sf.javaml.tools.weka.WekaException: java.lang.IllegalArgumentException: Value not defined for given nominal attribute!
at net.sf.javaml.tools.weka.WekaClassifier.classify(WekaClassifier.java:43)
at psofc2.MyClassifier.classify(MyClassifier.java:101)
at psofc2.TestingWithClassifier.fc(TestingWithClassifier.java:137)
at psofc2.Main.main(Main.java:399)
我需要知道这是怎么发生的,同时我为类标签有两个标称值。引起主要问题的与第99行相关的代码是
Object prediction = getClassifier().classify(instance);
与以下方法有关
public double classify(Dataset training, Dataset testing) {
getClassifier().buildClassifier(training);
Dataset dataForClassification = testing;
Map<Object, PerformanceMeasure> out = new HashMap<Object, PerformanceMeasure>();
for (Object o : training.classes()) {
out.put(o, new PerformanceMeasure());
}
for (Instance instance : dataForClassification) {
Object prediction = getClassifier().classify(instance);
if (prediction!=null){
if (instance.classValue().equals(prediction))
for (Object o : out.keySet()) {
if (o.equals(instance.classValue())) {
out.get(o).tp++;
} else {
out.get(o).tn++;
}
}
} else {
for (Object o : out.keySet()) {
/* prediction is positive class */
if (prediction.equals(o)) {
out.get(o).fp++;
} /* instance is positive class */ else if (o.equals(instance.classValue())) {
out.get(o).fn++;
} /* none is positive class */ else {
out.get(o).tn++;
}
}
}
}
}
double tp = 0.0, tn = 0.0;
double fp = 0.0, fn = 0.0;
double Accuracy = 0.0;
for (Object o : out.keySet()) {
tp += out.get(o).tp;
tn += out.get(o).tn;
fp += out.get(o).fp;
fn += out.get(o).fn;
}
Accuracy = (tn + tp) / (double) (out.size() * dataForClassification.size());
return Accuracy;
}
我的数据示例
@relation colon
@attribute f1 numeric
@attribute f2 numeric
@attribute f3 numeric
@attribute f4 numeric
@attribute f5 numeric
@attribute f6 numeric
@attribute f7 numeric
@attribute f8 numeric
@attribute f9 numeric
@attribute class {1,2}
@data
-2,-2,-2,-2,0,-2,0,-2,0,2
-2,-2,0,0,0,2,0,0,0,1
2,2,0,-2,-2,2,2,-2,0,2
-2,-2,-2,-2,-2,2,0,2,0,1