将 .arff 文件上传到 Eclipse

Uploading an .arff file into Eclipse

我正在尝试将 .arff 文件上传到我的 Eclipse IDE 以便我可以 运行 一些机器学习测试,并且真的希望社区帮助解决我的这个问题。

目的是创建一个静态方法 loadData,该方法将文件路径的字符串作为参数,returns 实例对象。

但是局部变量train的值由于某种原因没有被使用

我使用的代码:

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);
    }
}
}

.arff 文件的副本以实现再现性:

@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

我建议查看 how to use the Weka API 上的 Weka wiki 条目。

这里是对您的代码的快速修改:

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);
  }
}