3

我曾经FilteredClassifier创建一个模型。现在我想在测试集上评估模型。我可以通过 GUI 执行此操作,但是在使用 API 时,我的 java 程序在尝试调用classifyInstance()方法时会产生错误。

下面是我收到的测试文件、java程序和错误信息

测试文件:

@relation train-weka.filters.unsupervised.attribute.Remove-R2-weka.filters.unsupervised.attribute.NominalToString-C1

@attribute QUOTE string @attribute CAT {艺术,美丽,爱情,生活,知识,真相,最佳,力量,伟大,友谊,希望,力量,有趣}

@data '真正的艺术的特点是对创造性艺术家有一种不可抗拒的冲动。',?

Java代码:

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)
4

1 回答 1

0
unlabeled.instance(0)

似乎是空的,这就是您收到此异常的原因

于 2013-07-26T00:13:04.493 回答