计算属性中的实例数

Counting the number of instances from Attribute

我正在尝试计算 .arff 文件中特定属性的实例数。虽然,我似乎只能 select 属性而不是它出现的数据中的值。

在这种情况下,我试图 select 数据中出现 Wins 的次数,但是,代码中的值 Wins 仅 selects属性。

这是我正在使用的:

//create the class to load the data
package weka;

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_TRAIN1.arff";
    Instances train = loadData(dataLocation);
    System.out.println(train);
  }
}

//select for counts of values that appear in the data

public class test_learning {
public static void main(String[] args) throws Exception {
    String arff = "C:/Users/Emil/Downloads/Week 1/Arsenal_TRAIN1.arff";
    Instances data = DatasetLoading.loadData(arff);
    System.out.print("Num of Wins = " + data.attribute(2).value(2));

Output: Num of Wins = Win

预计:

Output: Num of Wins = 12

数据文件示例:

@relation Arsenal-weka.filters.unsupervised.attribute.Remove-R3

@attribute Leno {0,1}
@attribute Tierney {0,1}
@attribute class {Loss,Draw,Win}

@data
1,0,Loss
1,0,Loss
0,1,Draw
1,0,Draw
0,0,Win
0,1,Win
1,1,Win
0,1,Win
1,1,Win
1,0,Win
1,1,Loss
0,1,Draw
1,1,Draw
1,1,Draw
0,0,Win
1,0,Win
0,1,Win
1,1,Win
1,1,Win
1,1,Win

而不是检索第三个属性的第三个标称值 (Javadoc), you could use the Instances.attributeStats(int) method (Javadoc) to get the statistics for the third attribute (AttributeStats Javadoc)。