Search code examples
javaweka

Uploading an .arff file into Eclipse


I'm trying to upload a .arff file into my Eclipse IDE so that I can run some machine learning tests, and would really like the community to help to solve this problem with me.

The aim is to create a static method loadData that takes a String for the file path as an argument and returns an Instances object.

However, the value of the local variable train is not used for some reason.

The code I am using:

import weka.core.Instances;
import java.io.FileReader;



public class DatasetLoading {
public static void main(String[] args) {
    String dataLocation = "C:/Users/Emil/Downloads/Week 1/Arsenal_TRAIN.arff";
    Instances train;
    try {
        FileReader reader = new FileReader(dataLocation);
        train = new Instances(reader);
    } catch(Exception e) {
        System.out.println("Exception caught: "+e);
    }
}
}

Copy of the .arff file for reproducibility:

@RELATION Arsenal

@ATTRIBUTE Leno  {0,1}
@ATTRIBUTE Tierney   {0,1}
@ATTRIBUTE Saka  {0,1}
@ATTRIBUTE class    {Loss,Draw,Win}
@DATA

1, 0,  0,  Loss
1, 0,  0,  Loss
0, 1,  1,  Draw
1, 0,  1,  Draw
0, 0,  1,  Win
0, 1,  1,  Win
1, 1,  1,  Win
0, 1,  1,  Win
1, 1,  0,  Win
1, 0,  1,  Win
1, 1,  0,  Loss
0, 1,  0,  Draw
1, 1,  0,  Draw
1, 1,  0,  Draw
0, 0,  1,  Win
1, 0,  1,  Win
0, 1,  1,  Win
1, 1,  0,  Win
1, 1,  1,  Win
1, 1,  0,  Win

Solution

  • I recommend to have a look at the Weka wiki entry on how to use the Weka API.

    Here's a quick modification of your code:

    import weka.core.Instances;
    import weka.core.converters.ConverterUtils.DataSource;
    
    public class DatasetLoading {
    
      public static Instances loadData(String location) {
        try {
          return DataSource.read(location);
        }
        catch (Exception e) {
          System.err.println("Failed to load data from: " + location);
          e.printStackTrace();
          return null;
        }
      }
    
      public static void main(String[] args) {
        String dataLocation = "C:/Users/Emil/Downloads/Week 1/Arsenal_TRAIN.arff";
        Instances train = loadData(dataLocation);
        System.out.println(train);
      }
    }