【问题标题】:Counting the number of instances from Attribute从 Attribute 计算实例数
【发布时间】:2021-07-21 02:58:18
【问题描述】:

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

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

这是我正在使用的:

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

输出:获胜次数 = 获胜

预期:

输出:获胜次数 = 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

【问题讨论】:

    标签: java attributes instance arff


    【解决方案1】:

    您可以使用 Instances.attributeStats(int) 方法 (Javadoc) 获取第三个属性 (AttributeStats Javadoc) 的统计信息,而不是检索第三个属性 (Javadoc) 的第三个标称值。

    【讨论】:

    • 感谢您的回复,虽然我很难理解,这可能是我缺乏经验。当您提到Instances.attributeStats(int) 时,这是否意味着我必须创建一个构造函数来从属性中获取统计值?到目前为止,我已经尝试实现:data.attributeStats(2),但是,它返回:Type Nom Int Real Missing Unique Dist C[0] C[1] C[2] Nom 100% 0% 0% 0 / 0% 0 / 0% 3 3 5 12
    • 您当然需要从该对象中检索正确的统计信息,而不是仅仅输出 AttributeStats 对象的字符串表示形式 attributeStats(int) method returns (这就是我链接到 Javadoc 的原因)。要检索第三个属性的第三个标签的计数,您可能需要执行以下操作来检索该值:data.attributeStats(2).nominalCounts[2]
    • 你是个好老师!我正在自学 java 和 weka,你的解释很棒。我已经习惯了解释文档,您所有的 cmets 都非常有帮助,它们的工作方式与预期一样!
    猜你喜欢
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-26
    • 1970-01-01
    • 1970-01-01
    • 2011-08-04
    相关资源
    最近更新 更多