2014-02-16 3 views
4

Вот простой код, как в этом link прочитать файл ARFF в питона (комментируемого один не работает тоже):ImportError: модуль не назвал 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 
... 
+0

вы есть эта строка кода: 'импорта arff'? – ndpu

+0

Да, у меня было. Он просто не копировался. –

+0

На самом деле это вопрос, но не показывает !!!?! –

ответ

6

Вы должны переименовать скрипт arff.py к чему-то другому, например, arfftest.py. Python cant import arff, который вам нужен, потому что имя его совпадает с именем вашего файла приложения.

А если нету установлен arff сам пакет, установить его с пип или easy_install:

pip install arff 
# or easy_install arff 
# or pypm install arff 
+0

'Monas-MacBook-Pro: мона $ питон id3.py Traceback (самый последний вызов последнего): Файл "id3.py", строка 1, в импорт ARFF ImportError: Нет модуль с именем arff' –

+0

@MonaJalal эта ошибка означает, что python can not find arff. Вы его установили? – ndpu

+0

Спасибо, я установил его с помощью 'pypm install arff'. Я получаю эту ошибку:' python id3.py Traceback (последний последний звонок): Файл «id3.py», строка 3, в print (row.sex) Файл «/Users/mona/Library/Python/2.7/lib/python/site-packages/arff/__init__.py», строка 102, в __getattr__ объект возврата .__ getattr __ (self, key) AttributeError: тип объекта 'object' не имеет атрибута '__getattr __' ' –

2

После того как я изменить ваш, ARFF файл он работает:

NB: Я удалил сингл код из второго столбца атрибута.

Вот файл 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:~ 
Смежные вопросы