python - 名称错误 : name 'labels' is not defined

标签 python

我正在按照《使用 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/

相关文章:

python - 通过 python 和 cmd 查找 ip 地址之间的区别

python - 枚举数据加载器时出现 KeyError - 为什么?

python - matplotlib.pyplot.hist 错误的规范属性

python - 将 Pandas DataFrame 列转换为行

python - 格式错误的开始标记错误 - Python、BeautifulSoup 和 Sipie - Ubuntu 10.04

python - 处理大的json文件

python - 不懂 Python lambda 函数语法

python - numpy bincount - 选择最大权重而不是对所有权重求和

python - 与 numpy 操作 a[:] and a[. ..] 混淆

python - 如何将 JSON 字节串读入 pandas