2015-04-01 53 views
2

爲什麼有人會使用arff?請給出一個示例代碼來閱讀arff文件並在java中使用它。在arff後面的概念以及如何閱讀java中的weka arff?

我發現的代碼秧雞網站下面的代碼片段:

BufferedReader reader = 
new BufferedReader(new FileReader("/some/where/file.arff")); 
ArffReader arff = new ArffReader(reader); 
Instances data = arff.getData(); 
data.setClassIndex(data.numAttributes() - 1); 

之後是什麼?有人可以解釋上面發生了什麼嗎?我如何從文件訪問我的數據? weka網站提到了兩種不同的用法,即批量和增量。兩者有什麼區別?

回答

1

那麼,通常有人會使用arff,因爲它是一個非常簡單的文件格式,基本上是一個csv文件,頭文件描述數據,這是使用Weka保存/讀取數據的常用方法。

讀取arff文件的示例代碼正是您提供的示例代碼,如果要使用加載的實例,應該使用數據。要打印它們:System.out.println(data);您可以查看很多關於如何使用數據(分類,聚類等)here的示例。

您使用的代碼將arff文件加載到標準BufferedReader中,然後創建一個ArffReader實例(arff),該實例從reader中完全讀取數據,然後使用getData方法返回Instances對象中的數據(稱爲數據)。最後,你設置哪個屬性是類(arff文件中的最後一個)。

如果你想迭代實例對象,並檢索每個實例:

for (int i = 0; i <= data.numInstances - 1; i++) { 
    Instance instance = data.getInstance(i); 
    System.out.println(instance.stringValue(0)); //get Attribute 0 as String 
} 

您是從ARFF文件談論批量和增量讀取。批處理模式完全讀取arff文件,增量模式讓您有機會讀取arff文件的每個實例(行)並手動添加它。

代碼增量模式:

BufferedReader reader = 
    new BufferedReader(new FileReader("/some/where/file.arff")); 
ArffReader arff = new ArffReader(reader, 1000); 
Instances data = arff.getStructure(); 
data.setClassIndex(data.numAttributes() - 1); 
Instance inst; 
while ((inst = arff.readInstance(data)) != null) { 
    data.add(inst); 
} 
1

我發現內置的,因爲缺乏示例和很曖昧的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\'' 

希望它可以幫助別人

+0

是對代碼進行測試? – 2017-09-15 11:59:45

+0

是的,但僅限於我的具體情況,這不是一般的方法。這裏僅舉例說明如何閱讀arff對象。它可能無法處理某些特定情況。 – BaptisteL 2017-09-19 08:26:43