Python的arff库中的dump命令使用户能够根据给定的输入创建一个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是一个名义属性,即我希望我的arff头部如下所示:
@relation relation1
@attribute age real
@attribute hairColor **nominal**
@data
...发布于 2013-06-11 10:42:41
这里概述了几种不同的方法:
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')])https://stackoverflow.com/questions/16262633
复制相似问题