2017-04-25 2 views
0

저는 Python (Richart, Pedro Coelho) 도서로 Building Machine Learning Systems의 한 줄 씩의 예제를 따라 왔습니다.NameError : 이름 'labels'이 정의되지 않았습니다.

홍채 데이터 세트를 가져온 후, 우리는 나는이

>>> is_setosa = (labels == 'setosa') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'labels' is not defined 

내가 생각 가지고 Setosa

data = load_iris() 
features = data['data'] 
plength = features[:, 2] 
# use numpy operations to get setosa features 
is_setosa = (labels == 'setosa') 

으로 사람을 추출 할 것을 그래서 나는

>>> is_setosa = plenght(labels == 'setosa') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'plenght' is not defined 
>>> is_setosa = plength(labels == 'setosa') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'labels' is not defined 
>>> is_setosa = data(labels == 'setosa') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
NameError: name 'labels' is not defined 

을 시도 오타는 무엇입니까 이제해야 하나? 데이터 객체를 검사하려면 어떻게해야합니까?

>>> data.labels 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/home/milenko/miniconda2/lib/python2.7/site-packages/sklearn/datasets/base.py", line 58, in __getattr__ 
    raise AttributeError(key) 
AttributeError: labels 
>>> data.dtypes 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/home/milenko/miniconda2/lib/python2.7/site-packages/sklearn/datasets/base.py", line 58, in __getattr__ 
    raise AttributeError(key) 
AttributeError: dtypes 

이 설명

- class:\n    - Iris-Setosa\n    - Iris-Versicolour\n    - Iris-Virginica\n : 
+5

'labels'변수가 정의되어 있지 않습니다. – luoluo

+0

data [ 'labels'] 또는 data.labels ... 데이터 객체를 검사 할 가능성이 있습니다. – FLab

+0

'is_setosa' 변수에 부울 값이 없습니다. 이를 위해'labels == 'setosa'를 비교하고 있습니다. 이제'labels'은 정의되지 않았습니다. 위의 선언을'label = ""'과 같이 쓰고 싶습니까? – ishaan

답변

1

그냥 iPython 콘솔에 data를 입력하면 사용자가 데이터 세트에 대한 설명을 볼 수의 관련 부분이다. 특히 data['target']에는 data['target_names']에보고 된 이름에 해당하는 숫자 레이블 {0, 1, 2}, 즉 { 'setosa', 'versicolor', 'virginica'}가 들어 있습니다. 다음과 같이

그래서 당신은 아마 labels를 정의 할 수 있습니다 :

labels = map(lambda x: dict(enumerate(data['target_names']))[x], data['target']) 
0

은 주로 책의 코드 beacause를 "레이블을"정의의 부족이다. 문맥에 따라 다음 코드를 추가하십시오 :

target = data['target'] 
target_names = data['target_names'] 
labels=np.array([target_names[i] for i in target]) 
관련 문제