我正在构建一个Instances对象,添加属性,然后以Instance对象的形式添加数据。
当我写出来时,toString()方法已经抛出OutOfBoundsException并且无法评估Instances中的数据。当我尝试打印数据时收到错误,我可以看到在调试器中抛出异常,因为它显示它无法评估数据对象的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
答案 0 :(得分:0)
我终于明白了。我在构造函数中使用'true'第二个参数将Attributes设置为字符串,但它们是来自数据库表的整数。我需要更改我的行以将整数转换为字符串:
i.setValue(attributes.get(0), Integer.toString(r.studentIdentifier));
然而,这为我创造了一系列不同的问题,因为像Apriori算法这样的东西对字符串不起作用!我继续学习Weka。