已解决(机器学习中查看数据信息报错)AttributeError: target_names

成功解决(机器学习中查看数据信息报错):AttributeError: target_names




文章目录

  • 报错代码
  • 报错原因
  • 解决方法




报错代码


我的代码:

from sklearn import datasets

diabetes = datasets.load_diabetes(return_X_y=False, as_frame=True)
print(diabetes.target_names)

报错内容:

Traceback (most recent call last):
  File "D:\Python3.8\lib\site-packages\sklearn\utils\__init__.py", line 117, in __getattr__
    return self[key]
KeyError: 'target_names'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:/Python/1.py", line 6, in <module>
    print(diabetes.target_names)
  File "D:\Python3.8\lib\site-packages\sklearn\utils\__init__.py", line 119, in __getattr__
    raise AttributeError(key)
AttributeError: target_names

已解决(机器学习中查看数据信息报错)AttributeError: target_names_第1张图片



报错原因


target_names是标签名,sciket-learn数据集API中,回归数据集没有标签名,其他数据集才有



解决方法


查看其他内容如:特征名、新闻数据、手写数字

from sklearn import datasets

diabetes = datasets.load_diabetes(return_X_y=False, as_frame=True)
print(diabetes.feature_names)

运行结果:

['age', 'sex', 'bmi', 'bp', 's1', 's2', 's3', 's4', 's5', 's6']

你可能感兴趣的:(《告别Bug》,python,开发语言,sklearn,机器学习)