8

这是一个简单的代码,如此链接中的一个简单代码,用于在 python 中读取 arff 文件(注释的那个也不起作用):

import arff
for row in arff.load('heart_train.arff'):
        print(row.sex)  

这是我收到的错误:

python id3.py 
Traceback (most recent call last):
  File "id3.py", line 1, in <module>
    import arff
ImportError: No module named arff

“heart_train” arff 文件数据如下:

@relation cleveland-14-heart-disease
@attribute 'age' real
@attribute 'sex' { female, male}
@attribute 'cp' { typ_angina, asympt, non_anginal, atyp_angina}
@attribute 'trestbps' real
@attribute 'chol' real
@attribute 'fbs' { t, f}
@attribute 'restecg' { left_vent_hyper, normal, st_t_wave_abnormality}
@attribute 'thalach' real
@attribute 'exang' { no, yes}
@attribute 'oldpeak' real
@attribute 'slope' { up, flat, down}
@attribute 'ca' real
@attribute 'thal' { fixed_defect, normal, reversable_defect}
@attribute 'class' { negative, positive}
@data
63,male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,negative
37,male,non_anginal,130,250,f,normal,187,no,3.5,down,0,normal,negative
41,female,atyp_angina,130,204,f,left_vent_hyper,172,no,1.4,up,0,normal,negative
56,male,atyp_angina,120,236,f,normal,178,no,0.8,up,0,normal,negative
57,female,asympt,120,354,f,normal,163,yes,0.6,up,0,normal,negative
57,male,asympt,140,192,f,normal,148,no,0.4,flat,0,fixed_defect,negative
...
4

2 回答 2

12

例如,您应该将脚本重命名arff.py为不同arfftest.py的名称。Python 无法导入arff您需要的模块,因为它的名称与您的应用程序文件名相同。

如果您还没有安装arff包本身,请使用 pip 或 easy_install 安装它:

pip install arff
# or easy_install arff
# or pypm install arff
于 2014-02-16T11:51:39.160 回答
2

在我更改您的 arff 文件后,它可以工作:

注意:我从属性的第二列中删除了单个代码。

这是arff文件:

@relation cleveland-14-heart-disease
@attribute age real
@attribute sex { female, male}
@attribute cp { typ_angina, asympt, non_anginal, atyp_angina}
@attribute trestbps real
@attribute chol real
@attribute fbs { t, f}
@attribute restecg { left_vent_hyper, normal, st_t_wave_abnormality}
@attribute thalach real
@attribute exang { no, yes}
@attribute oldpeak real
@attribute slope { up, flat, down}
@attribute ca real
@attribute thal { fixed_defect, normal, reversable_defect}
@attribute class { negative, positive}
@data
63,male,typ_angina,145,233,t,left_vent_hyper,150,no,2.3,down,0,fixed_defect,negative
37,male,non_anginal,130,250,f,normal,187,no,3.5,down,0,normal,negative
41,female,atyp_angina,130,204,f,left_vent_hyper,172,no,1.4,up,0,normal,negative
56,male,atyp_angina,120,236,f,normal,178,no,0.8,up,0,normal,negative
57,female,asympt,120,354,f,normal,163,yes,0.6,up,0,normal,negative
57,male,asympt,140,192,f,normal,148,no,0.4,flat,0,fixed_defect,negative

片段和输出:

:/tmp:~ cat a.py
import arff
for row in arff.load('heart_train.arff'):
   print(row.sex)
:/tmp:~ python a.py
male
male
female
male
female
male
:/tmp:~
male
:/tmp:~
于 2014-02-16T12:52:41.027 回答