【发布时间】: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