【问题标题】:Issue with Weka core DenseInstanceWeka 核心 DenseInstance 的问题
【发布时间】:2017-11-27 11:01:46
【问题描述】:

我正在构建一个 Instances 对象,添加 Attributes,然后以 Instance 对象的形式添加数据。

当我去写它时,toString() 方法已经抛出 OutOfBoundsException 并且无法评估实例中的数据。当我尝试打印数据时收到错误消息,我可以看到仅在调试器中引发的异常,因为它表明它无法评估数据对象的 toString()。

我唯一的线索是错误消息似乎使用了第一个数据元素 (StudentId) 并将其用作索引。我很困惑为什么。

代码:

// Set up the attributes for the Weka data model
ArrayList<Attribute> attributes = new ArrayList<>();
attributes.add(new Attribute("StudentIdentifier", true));
attributes.add(new Attribute("CourseGrade", true));
attributes.add(new Attribute("CourseIdentifier"));
attributes.add(new Attribute("Term", true));
attributes.add(new Attribute("YearCourseTaken", true));

// Create the data model object - I'm not happy that capacity is required and fixed? But that's another issue
Instances dataSet = new Instances("Records", attributes, 500);
// Set the attribute that will be used for prediction purposes - that will be CourseIdentifier
dataSet.setClassIndex(2);

// Pull back all the records in this term range, create Weka Instance objects for each and add to the data set
List<Record> records = recordsInTermRangeFindService.find(0, 10);
int count = 0;
for (Record r : records) {
    Instance i = new DenseInstance(attributes.size());

    i.setValue(attributes.get(0), r.studentIdentifier);
    i.setValue(attributes.get(1), r.courseGrade);
    i.setValue(attributes.get(2), r.courseIdentifier);
    i.setValue(attributes.get(3), r.term);
    i.setValue(attributes.get(4), r.yearCourseTaken);

    dataSet.add(i);
}

System.out.println(dataSet.size());
BufferedWriter writer = null;
try {
    writer = new BufferedWriter(new FileWriter("./test.arff"));
    writer.write(dataSet.toString());
    writer.flush();
    writer.close();
} catch (IOException e) {
    e.printStackTrace();
}

错误信息:

Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1010, Size: 0

【问题讨论】:

    标签: weka


    【解决方案1】:

    我终于明白了。我在构造函数中使用“true”第二个参数将属性设置为字符串,但它们是来自数据库表的整数。我需要更改行以将整数转换为字符串:

    i.setValue(attributes.get(0), Integer.toString(r.studentIdentifier));
    

    但是,这给我带来了一系列不同的问题,因为诸如 Apriori 算法之类的东西不适用于字符串!我将继续学习 Weka。

    【讨论】:

      猜你喜欢
      • 2017-02-09
      • 2015-05-20
      • 2011-07-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多