我發現內置的,因爲缺乏示例和很曖昧的javadoc讀者很難。所以這裏是我寫的一小段代碼,用於讀取經過測試和工作的數字屬性關係的關係!
BufferedReader reader = new BufferedReader(new FileReader(new File(path)));
ArffReader arff = new ArffReader(reader, 1000);
Instances data = arff.getStructure();
data.setClassIndex(0);
Instance inst;
while ((inst = arff.readInstance(data)) != null) {
// the first attribute is ignored because it is the index
for(int i = 1 ; i < inst.numAttributes() ; i++) {
switch(inst.attribute(index).type()) {
case Attribute.NUMERIC :
System.out.println(inst.value(index));
case Attribute.STRING :
System.out.println(inst.stringValue(index));
case Attribute.RELATIONAL :
// test if we have an imbrication of two relations or not
if (inst.attribute(index).relation().numAttributes() > 0 &&
inst.attribute(index).relation().attribute(0).isRelationValued()) {
inst.attribute(index).relation().attribute(0).isRelationValued()) {
// case of an array of int arrays
double[][] seq = new double[inst.attribute(index).relation().numAttributes()][];
for (int i = 0 ; i < inst.attribute(index).relation().numAttributes() ; i++) {
Instances instances = inst.relationalValue(index);
seq[i] = new double[instances.attribute(0).relation().numAttributes()];
Instance q = instances.instance(0).relationalValue(i).get(0);
for(int j = 0 ; j < instances.attribute(0).relation().numAttributes() ; j++) {
seq[i][j] = q.value(j);
}
}
System.out.println(seq);
} else {
// case wit only an arry of int
double[] seq = new double[inst.attribute(index).relation().numAttributes()];
for (int i = 0 ; i < inst.attribute(index).relation().numAttributes() ; i++) {
seq[i] = inst.value(i);
}
System.out.println(seq);
}
}
}
System.out.println("index is : "+((int) inst.value(0)));
}
下面是數據的模樣,每個元素組成的指數,和一對數值三胞胎:
@relation 'name of relation'
@attribute index numeric
@attribute attr1 relational
@attribute attr1.0 relational
@attribute attr1.0.0 numeric
@attribute attr1.0.1 numeric
@attribute attr1.0.2 numeric
@end attr1.0
@attribute attr1.1 relational
@attribute attr1.1.0 numeric
@attribute attr1.1.1 numeric
@attribute attr1.1.2 numeric
@end attr1.1
@end attr1
@data
0,'\'23,25,48\',\'12,0,21\''
115260,'\'34,44,72\',\'15,8,32\''
230520,'\'175,247,244\',\'107,185,239\''
345780,'\'396,269,218\',\'414,276,228\''
461040,'\'197,38,42\',\'227,40,43\''
希望它可以幫助別人
是對代碼進行測試? – 2017-09-15 11:59:45
是的,但僅限於我的具體情況,這不是一般的方法。這裏僅舉例說明如何閱讀arff對象。它可能無法處理某些特定情況。 – BaptisteL 2017-09-19 08:26:43