Re: ArrayIndexOutOfBoundsException when classifying an instance

Peter Reutemann <[email protected]>
Newsgroups gmane.comp.ai.weka
Message-ID <CAHoQ12+nZD5xCRoNzL03VKseUMXQUZkMemV8ExCNh9x-jRvDAg@mail.gmail.com>
> I am trying to classify instances and code is given below.
>
> public void prepare_data() throws IOException {
>                 BufferedReader buf_reader=new BufferedReader(new
> FileReader(this.train_file));
>
>                 Instances train_data = new Instances(buf_reader);
>                 train_data.setClassIndex(train_data.numAttributes() - 1);
>                 this.train=train_data;
>                 buf_reader.close();
>
>                 buf_reader=new BufferedReader(new FileReader(this.test_file));
>
>                 Instances test_data = new Instances(buf_reader);
>                 test_data.setClassIndex(test_data.numAttributes() - 1);
>                 this.test=test_data;
>                 buf_reader.close();
>         }
>
> public void filteredFier() throws Exception {
>
>                 System.out.println("hi"+ this.train.equalHeaders(this.test));
>                 NumericToNominal nn = new NumericToNominal();
>                 String[] options= {"-R", "first-last"};
>                 nn.setOptions(options);
>                 nn.setInputFormat(this.train);
>                 this.rf=new RandomForest();
>                 this.fc = new FilteredClassifier();
>                 this.fc.setFilter(nn);
>
>                 this.fc.setClassifier(this.rf);
>
>                 this.fc.buildClassifier(this.train);
>
>                 Evaluation eval=new Evaluation(this.train);
>
>
>                 eval.crossValidateModel(this.fc,this.train,10,new Random(1));
>
>                 System.out.println(eval.toSummaryString());
>
>
>
>
>         }
>         public void get_accuracy() throws Exception {
>
>
>
>                 for (int i = 0; i < this.test.numInstances(); i++) {
>                         double clsLabel = this.fc.classifyInstance(this.test.instance(i));
>                         System.out.println(clsLabel);
>                 }
>
>
>
>
>         }
>
> I get ArrayIndexOutOfBoundException. Somebody here knows solution, I need
> your help. Thank you in advance.

You need to post the full stacktrace. With the information that you
provided, we don't know where the error originated.

I took your code and stream-lined it a bit. Run it with your
train/test set and post the full stacktrace if you still get the
ArrayIndexOutOfBoundsException.
The class expects two command-line arguments: train and test file.

Cheers, Peter
-- 
Peter Reutemann
Dept. of Computer Science
University of Waikato, NZ
+64 (7) 577-5304
http://www.cms.waikato.ac.nz/~fracpete/
http://www.data-mining.co.nz/

_______________________________________________
Wekalist mailing list -- [email protected]
Send posts to [email protected]
To unsubscribe send an email to [email protected]
To subscribe, unsubscribe, etc., visit https://list.waikato.ac.nz/postorius/lists/wekalist.list.waikato.ac.nz
List etiquette: http://www.cs.waikato.ac.nz/~ml/weka/mailinglist_etiquette.html
Something.java (text/x-java, 2.8 KB)
import weka.classifiers.AbstractClassifier;
import weka.classifiers.Classifier;
import weka.classifiers.Evaluation;
import weka.classifiers.evaluation.Prediction;
import weka.classifiers.meta.FilteredClassifier;
import weka.classifiers.trees.RandomForest;
import weka.core.Attribute;
import weka.core.Instances;
import weka.core.converters.ConverterUtils.DataSource;
import weka.filters.unsupervised.attribute.NumericToNominal;

import java.util.Random;

public class Something {

  private String trainFile;
  private Instances train;
  private String testFile;
  private Instances test;
  private FilteredClassifier fc;

  public Something(String trainFile, String testFile) {
    this.trainFile = trainFile;
    this.testFile = testFile;
  }

  public void loadData() throws Exception {
    train = DataSource.read(trainFile);
    train.setClassIndex(train.numAttributes() - 1);

    test = DataSource.read(testFile);
    test.setClassIndex(test.numAttributes() - 1);

    System.out.println("train/test compatible? " + train.equalHeaders(test));
  }

  public Instances getTrain() {
    return train;
  }

  public Instances getTest() {
    return test;
  }

  public void setupClassifier() throws Exception {
    NumericToNominal nn = new NumericToNominal();
    String[] options = {"-R", "first-last"};
    nn.setOptions(options);
    nn.setInputFormat(train);
    RandomForest rf = new RandomForest();
    fc = new FilteredClassifier();
    fc.setFilter(nn);
    fc.setClassifier(rf);
  }

  public Evaluation crossValidate() throws Exception {
    Evaluation eval = new Evaluation(train);
    eval.crossValidateModel(fc, train, 10, new Random(1));
    System.out.println(eval.toSummaryString("\n=== Cross-validation on train ===", false));
    return eval;
  }

  public Evaluation evaluate() throws Exception {
    Classifier cls = AbstractClassifier.makeCopy(fc);
    cls.buildClassifier(train);
    Evaluation eval = new Evaluation(train);
    eval.evaluateModel(cls, test);
    System.out.println(eval.toSummaryString("\n=== Evaluation on test ===", false));
    return eval;
  }

  public static void main(String[] args) throws Exception {
    Evaluation eval;
    Something s = new Something(args[0], args[1]);
    s.loadData();
    s.setupClassifier();

    // cross-validate on train
    eval = s.crossValidate();
    System.out.println("Accuracy (cross-validation): "  + eval.pctCorrect());

    // build on train and evaluate on test
    eval = s.evaluate();
    System.out.println("Accuracy (test): "  + eval.pctCorrect());

    // output predictions on test
    System.out.println("\nPredictions (actual -> predicted)");
    Attribute clsAtt = s.getTrain().classAttribute();
    for (Prediction p: eval.predictions()) {
      System.out.println(clsAtt.value((int) p.actual()) + " -> " + clsAtt.value((int) p.predicted()));
    }
  }
}
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.