2013-04-28 53 views
0

Python中的arff庫中的dump命令使用戶能夠根據給定的輸入創建arff文件,例如,命令:由python中的arff庫創建的arff文件中的名義屬性

arff.dump("outputDir", data, relation="relation1", 
      names=['age, fatRatio, hairColor']) 

產生以下ARFF:

@relation relation1 
@attribute age real 
@attribute hairColor string 
@data 
10,0.2,black 
22,10,yellow 
30,2,black 

的數據給出:

data = [[10,0.2,'black'],[22,10,'yellow'],[30,2,'black']] 

我的問題是:如何通知我想hairColor到相關機制是一個名義上的屬性,即我希望我的阿爾菲標題如下:

@relation relation1 
@attribute age real 
@attribute hairColor **nominal** 
@data 
... 

回答

0

有幾種不同的方式在這裏做這樣概括:

https://code.google.com/p/arff/wiki/Documentation

我覺得對我來說更好的方法是第二個這建議是:

arff_writer = arff.Writer(fname, relation='diabetics_data', names) 
arff_writer.pytypes[arff.nominal] = '{not_parasite,parasite}' 
arff_writer.write([arff.nominal('parasite')]) 

如果你看看在代碼爲arff.nominal,它的定義是這樣的:

class Nominal(str): 
    """Use this class to wrap strings which are intended to be nominals 
    and shouldn't have enclosing quote signs.""" 
    def __repr__(self): 
     return self 

所以我有大功告成創建每個標稱不同的「包裝」標稱類屬性我這樣的:

class ZipCode(str): 
    """Use this class to wrap strings which are intended to be nominals 
    and shouldn't have enclosing quote signs.""" 
    def __repr__(self): 
     return self 

,然後按照上面的代碼,你可以做這樣的事情:

arff_writer = arff.Writer(fname, relation='neighborhood_data', names) 
arff_writer.pytypes[type(myZipCodeObject)] = '{85104,84095}' 
# then write out the rest of your attributes... 

arff_writer.write([arff.nominal('parasite')])