输出Instances()[WEKA]的IndexOutofBounds错误

时间:2014-01-12 17:25:34

标签: java eclipse machine-learning weka indexoutofboundsexception

我在这里有一堆Java代码行,在尝试执行此操作时抛出索引outofbounds异常

System.out.println(data3) //during the loop that fills the object
  • data3是Class Instances()
  • 的对象
  • 据我所知,其中没有涉及索引
  • System.out.println(data3)在填充Instances()对象的循环的第二次迭代后失败
  • 如果您// - 注释data3的sysout并使用

    直接访问数据

    的System.out.println(data3.instance(6).classValue());

您看到Instances对象已完全填满,一切正常。为什么我会收到错误

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
    at weka.core.FastVector.elementAt(FastVector.java:254)
    at weka.core.Attribute.value(Attribute.java:692)
    at weka.core.Instance.stringValue(Instance.java:846)
    at weka.core.Instance.stringValue(Instance.java:822)
    at weka.core.Instance.toString(Instance.java:942)
    at weka.core.Instance.toString(Instance.java:883)
    at java.lang.String.valueOf(Unknown Source)
    at java.lang.StringBuffer.append(Unknown Source)
    at weka.core.Instances.stringWithoutHeader(Instances.java:1376)
    at weka.core.Instances.toString(Instances.java:1360)
    at org.tuhh.cpmgg.weka.AudioClassifier.main(AudioClassifier.java:143)

这是我的代码(你需要jaudio 1.0.4和weka 3.8和JRE 1.7)

package org.tuhh.cpmgg.weka;

import java.io.File;
import java.io.FileFilter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.regex.Pattern;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import a.main.JAudioLaunch;
import jAudioFeatureExtractor.ACE.DataTypes.Batch;
import jAudioFeatureExtractor.DataTypes.RecordingInfo;
import jAudioFeatureExtractor.jAudioTools.AudioSamples;
import weka.core.*;
public class AudioClassifier {

    public static File[] listFilesMatching(File root, String regex) {
        if(!root.isDirectory()) {
            throw new IllegalArgumentException(root+" is no directory.");
        }
        final Pattern p = Pattern.compile(regex);
        return root.listFiles(new FileFilter(){
        //    @Override
            public boolean accept(File file) {
                return p.matcher(file.getName()).matches();
            }
        });
    }

    public static void main (String[] args){

        int z = 0;
        try {
            File[] definers = listFilesMatching(new File("src\\main\\java\\org\\tuhh\\cpmgg\\weka\\"), "^f.*.xml");
            System.out.println(definers[7].getName());

            JAXBContext jaxbContext = JAXBContext.newInstance(FeatureVectorFile.class);
            Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();

            FastVector atts = new FastVector(8);
            // Create Nominal Attribute for Genres
            FastVector genres = new FastVector();
            genres.addElement("Jazz");
            genres.addElement("Country");
            genres.addElement("Rock");
            genres.addElement("Hiphop");
            genres.addElement("Ska");
            genres.addElement("Other");
            genres.addElement("Blues");
            genres.addElement("Randb");
            genres.addElement("Pop");
            genres.addElement("Electronic");
            //Create Feature Attributes
            atts.addElement(new Attribute("Spectral Flux Overall Standard Deviation"));
            atts.addElement(new Attribute("Root Mean Square Overall Standard Deviation"));
            atts.addElement(new Attribute("Beat Sum Overall Standard Deviation"));
            atts.addElement(new Attribute("Method of Moments Overall Standard Deviation"));
            atts.addElement(new Attribute("Spectral Flux Overall Average"));
            atts.addElement(new Attribute("Root Mean Square Overall Average"));
            atts.addElement(new Attribute("Beat Sum Overall Average"));
            atts.addElement(new Attribute("Method of Moments Overall Average"));
            atts.addElement(new Attribute("Genre ",genres));
            Instances data3 = new Instances("GenreDefiners", atts, 0);
            data3.setClassIndex(data3.numAttributes()-1);

            System.out.println("ZIP " + data3);

            for(File itfile:definers){

                System.out.println("ITERATION " + z);

                FeatureVectorFile featuress = (FeatureVectorFile) jaxbUnmarshaller.unmarshal(itfile);
                List<Feature>  fvalss = featuress.dataset.getFeatures();

                //Read Attribute/Feature Values from parsed xml
                double[] vals = new double[data3.numAttributes()];
                int y=0;
                for(Feature d:fvalss) {
                    vals[y] = Double.parseDouble(d.values.get(0));
                        y=y+1;
                    }

                String currentclass = itfile.getName().replaceAll("[^A-Z]", "");
                switch (currentclass) {
                case "JAZZ":
                            vals[8] = 1.0;
                            data3.add(new Instance(1.0,vals));
                            break;
                case "COUNTRY":
                            vals[8] = 2.0;
                            data3.add(new Instance(1.0,vals));
                            break;
                case "ROCK":
                            vals[8] = 3.0;
                            data3.add(new Instance(1.0,vals));
                            break;
                case "HIPHOP":
                            vals[8] = 4.0;
                            data3.add(new Instance(1.0,vals));
                            break;
                case "SKA":
                            vals[8] = 5.0;
                            data3.add(new Instance(1.0,vals));
                            break;
                case "OTHER":
                            vals[8] = 6.0;
                            data3.add(new Instance(1.0,vals));
                            break;
                case "BLUES":
                            vals[8] = 7.0;
                            data3.add(new Instance(1.0,vals));
                            break;
                case "RANDB":
                            vals[8] = 8.0;
                            data3.add(new Instance(1.0,vals));
                            break;
                case "POP": 
                            vals[8] = 9.0;
                            //System.out.println(z);
                            //System.out.println(itfile.getName());
                            data3.add(new Instance(1.0,vals));
                            //System.out.println(data3.instance(z).classValue());
                            break;
                case "ELECTRONIC":
                            vals[8] = 10.0;
                            data3.add(new Instance(1.0,vals));
                            break;
                }
                //data3.add(new Instance(1.0,vals));
                /*System.out.println("# Attrs");
                System.out.println(data3.numAttributes());
                System.out.println("# Classes");
                System.out.println(data3.numClasses());
                System.out.println("Classvalue");
                System.out.println(data3.instance(z).classValue());*/
                z=z+1;
            }
            System.out.println(data3.instance(6).classValue());
            System.out.println(data3.toString()); //THIS FAILS... but only during 2nd iteration...

              } catch (JAXBException e) {
            e.printStackTrace();
              }
    }

    @SuppressWarnings("unused")
    private static File privjanalyse(File argum) {
        /*
         * Yijun: the input file is already in the test batch file to extract the corresponding features
         * Todo: file as argument, no batch file use, feedback of featurevector
         */
        File musicFile = new File(argum.getAbsolutePath());
        JAudioLaunch launch = new JAudioLaunch(musicFile);

        String [] string = new String[2];
        string[0] = "-b";
        string[1] = "C:\\Users\\ce993\\Dropbox\\Project Laborotary\\jAudio\\batch"; //    src/main/resources/JAudio
    //  JAudioCommandLine.execute(string);

        File featurefile = new File("C:\\Users\\ce993\\Dropbox\\Project Laborotary\\jAudio\\feature_values_current.xml");
        return featurefile;
    }

}

0 个答案:

没有答案