python - error when try to incorporate target data from SKLEARN in python
问题描述
I am trying to build a matrix to evaluate how different features' impact the data set's target, here I use Sklearn's breast cancer data, my code is below but the result shows me error, I could not figure out how to fix it can someone help me here?
import numpy as np
import seaborn as sns; sns.set(style="ticks", color_codes=True)
import sklearn.datasets
import pandas as pd
from sklearn.datasets import load_breast_cancer
WBC_dataset = load_breast_cancer()
WBC_df = pd.DataFrame(
data= np.c_[WBC_dataset['data'],WBC_dataset['target']],
columns= np.append(WBC_dataset['feature_names'], ['Condition']))
cols = WBC_dataset.columns.drop('Condition')
WBC_df[cols] = WBC_df[cols].apply(pd.to_numeric)
g = sns.pairplot(WBC_df, hue='Condition')
here is the error
KeyError Traceback (most recent call last)
~\Anaconda3\lib\site-packages\sklearn\utils\__init__.py in __getattr__(self, key)
104 try:
--> 105 return self[key]
106 except KeyError:
KeyError: 'columns'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-245-1123cb0b25bb> in <module>
9 data= np.c_[WBC_dataset['data'],WBC_dataset['target']],
10 columns= np.append(WBC_dataset['feature_names'], ['Condition']))
---> 11 cols = WBC_dataset.columns.drop('Condition')
12 WBC_df[cols] = WBC_df[cols].apply(pd.to_numeric)
13 g = sns.pairplot(WBC_df, hue='Condition')
~\Anaconda3\lib\site-packages\sklearn\utils\__init__.py in __getattr__(self, key)
105 return self[key]
106 except KeyError:
--> 107 raise AttributeError(key)
108
109 def __setstate__(self, state):
AttributeError: columns
解决方案
WBC_df 不是数据框,它是一个包含多个值的字典。检查文档。
data = load_breast_cancer()
WBC_df = pd.DataFrame(data.data,columns = data.feature_names)
推荐阅读
- angularjs - Angular 性能升级没有 $rootscope、$location 的提供者
- python - 使用 group by 来获取 n 个最小值但有重复项
- angular - 在 Angular 中使用 Highcharts (>= 8.0.1) 函数 stockChart() 时出错
- python - 如何将完整的代码存储在变量中
- c - C 中的 const 数据修改
- javascript - 为更新数据库反应更新对象值(数量)
- cakephp - Cakephp .json ext 给 Missing Method
- c# - 为什么我的应用程序可以在我的计算机上运行,但不能在其他计算机上运行
- python - 如何在不询问两次输入的情况下将输入函数传递给另一个函数
- javascript - 有没有办法与 facebook api 共享 html5 画布图像?