我正在按照《使用 Python 构建机器学习系统》(Ricart、Pedro Coelho)书中的逐行示例进行操作。
导入鸢尾花数据集后,我们想用Setosa提取鸢尾花数据集
data = load_iris()
features = data['data']
plength = features[:, 2]
# use numpy operations to get setosa features
is_setosa = (labels == 'setosa')
我明白了
>>> is_setosa = (labels == 'setosa')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'labels' is not defined
我猜这是错字,所以我尝试了
>>> 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 :
最佳答案
如果您只需在 iPython 控制台中输入 data
,您将看到数据集的描述。特别是有两个字段:data['target']
包含数字标签 {0, 1, 2},对应于 data['target_names']
中报告的名称,即 {'setosa'、'versicolor'、'virginica'}。
因此您可以按如下方式定义标签
:
labels = map(lambda x: dict(enumerate(data['target_names']))[x], data['target'])
关于python - 名称错误 : name 'labels' is not defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43609595/