9

Here's a simple code as in this link to read an arff file in python (the commented one didn't work too):

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

And here's the error I receive:

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 file data is like:

@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
...
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mona Jalal
  • 34,860
  • 64
  • 239
  • 408

2 Answers2

14

You should rename your script from arff.py to something different, arfftest.py for example. Python cant import arff module you need because name of it is the same as your app file name.

And if you havent installed arff package itself, install it with pip or easy_install:

pip install arff
# or easy_install arff
# or pypm install arff
ndpu
  • 22,225
  • 6
  • 54
  • 69
  • `Monas-MacBook-Pro: mona$ python id3.py Traceback (most recent call last): File "id3.py", line 1, in import arff ImportError: No module named arff` – Mona Jalal Feb 16 '14 at 11:55
  • @MonaJalal that error means that python cant find arff. Did you installed it? – ndpu Feb 16 '14 at 11:57
  • Thanks I installed it using `pypm install arff` I am receiving this error: ` python id3.py Traceback (most recent call last): File "id3.py", line 3, in print(row.sex) File "/Users/mona/Library/Python/2.7/lib/python/site-packages/arff/__init__.py", line 102, in __getattr__ return object.__getattr__(self, key) AttributeError: type object 'object' has no attribute '__getattr__'` – Mona Jalal Feb 16 '14 at 12:04
  • which python version you ve ? its Tested on python 2.7 and 3.2 – James Sapam Feb 16 '14 at 12:30
2

After I change your, arff file it works:

NB: I removed the single code from the second column of the attribute.

Here is the arff file:

@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

Snippet and output:

:/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:~
James Sapam
  • 16,036
  • 12
  • 50
  • 73