Richard Rublev
Richard Rublev

Reputation: 8172

NameError: name 'labels' is not defined

I am following line by line example from Building Machine Learning Systems with Python(Richart,Pedro Coelho) book.

After importing iris data sets,we want to extract ones with Setosa

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

I got this

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

I guess that is typo so I tried

>>> 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

What should I do now? How can I inspect data object?

>>> 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

This is the relevant part of the description

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

Upvotes: 1

Views: 17632

Answers (2)

Uncle LAU
Uncle LAU

Reputation: 13

it mainly beacause the code in the book is lack of defining "labels". Based on the context, add the following codes:

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

Upvotes: 0

FLab
FLab

Reputation: 7476

If you just type data into the iPython console you will see a description of the dataset. In particular there are two fields: data['target'] contains numerical labels {0, 1, 2} which corresponds to the names reported in data['target_names'], i.e. {'setosa', 'versicolor', 'virginica'}.

So you can probably define labels as follows:

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

Upvotes: 1

Related Questions