我使用FilteredClassifier
来创建模型。现在我想在测试集上评估模型。
我可以通过GUI完成此操作,但在使用API时,我的java程序在尝试调用classifyInstance()
方法时会产生错误。
以下是我收到的测试文件,java程序和错误消息
@relation train-weka.filters.unsupervised.attribute.Remove-R2-weka.filters.unsupervised.attribute.NominalToString-C1
@attribute QUOTE字符串 @attribute CAT {艺术,美丽,爱情,生活,知识,真理,最佳,力量,伟大,友谊,希望,力量,有趣}
@data “真正的艺术的特点是创作艺术家不可抗拒的冲动。”,?
public class test {
public static void main(String[] args) throws FileNotFoundException, Exception {
// TODO Auto-generated method stub
String fileName = "./tree.model";
ObjectInputStream in = new ObjectInputStream(new FileInputStream(fileName));
Object tmp = in.readObject();
FilteredClassifier tree = (FilteredClassifier) tmp;
in.close();
System.out.println("===== Loaded model: " + fileName + " =====");
// load unlabeled data
Instances unlabeled = new Instances(new BufferedReader(new FileReader("./test3.arff")));
System.out.println(unlabeled);
// set class attribute
unlabeled.setClassIndex(unlabeled.numAttributes() - 1);
// create copy
Instances labeled = new Instances(unlabeled);
// label instances
for (int i = 0; i < unlabeled.numInstances(); i++) {
double clsLabel = tree.classifyInstance(unlabeled.instance(i));
labeled.instance(i).setClassValue(clsLabel);
}
// save labeled data
BufferedWriter writer = new BufferedWriter(new FileWriter("/some/where/labeled.arff"));
writer.write(labeled.toString());
writer.newLine();
writer.flush();
writer.close();
}
}
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.rangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at weka.core.Instances.attribute(Instances.java:341)
at weka.core.AttributeLocator.locate(AttributeLocator.java:153)
at weka.core.AttributeLocator.initialize(AttributeLocator.java:119)
at weka.core.AttributeLocator.<init>(AttributeLocator.java:102)
at weka.core.StringLocator.<init>(StringLocator.java:69)
at weka.filters.Filter.flushInput(Filter.java:431)
at weka.filters.unsupervised.attribute.StringToWordVector.batchFinished(StringToWordVector.java:768)
at weka.classifiers.meta.FilteredClassifier.filterInstance(FilteredClassifier.java:474)
at weka.classifiers.meta.FilteredClassifier.distributionForInstance(FilteredClassifier.java:495)
at weka.classifiers.AbstractClassifier.classifyInstance(AbstractClassifier.java:70)
at test.main(test.java:43)
答案 0 :(得分:0)
unlabeled.instance(0)
似乎是空的,这是你得到这个异常的原因