[英]I am getting an attribute error for an attribute defined inside the class. How can I fix this?
[英]Why am I getting an attribute error? How can I fix it?
from sklearn import datasets
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
diabetes = datasets.load_diabetes()
X = diabetes.data
y = diabetes.target
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0)
mms = MinMaxScaler()
X_train_norm = mms.fit_transform(X_train)
X_test_norm = mms.transform(X_test)
stdsc = StandardScaler()
X_train_std = stdsc.fit_transform(X_train)
X_test_std = stdsc.transform(X_test)
LogisticRegression(penalty='l1')
lr = LogisticRegression(penalty='l1', C=1.0, multi_class='auto', solver='liblinear')
lr.fit(X_train_std, y_train)
print('Training accuracy:', lr.score(X_train_std, y_train))
print('Test accuracy:', lr.score(X_test_std, y_test))
from sklearn.ensemble import RandomForestClassifier
forest = RandomForestClassifier(n_estimators=500, random_state=1)
forest.fit(X_train, y_train)
importances = forest.feature_importances_
print(importances)
feature_labels = diabetes.columns[0:]
print(feature_labels)
這是我得到的錯誤。 任何人都可以幫忙嗎?
KeyError Traceback (most recent call last)
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/__init__.py in __getattr__(self, key)
95 try:
---> 96 return self[key]
97 except KeyError:
KeyError: 'columns'
During handling of the above exception, another exception occurred:
AttributeError Traceback (most recent call last)
<ipython-input-163-74a2bb3a96b9> in <module>
9 print(importances)
10
---> 11 feature_labels = diabetes.columns[0:]
12 print(feature_labels)
13
~/opt/anaconda3/lib/python3.8/site-packages/sklearn/utils/__init__.py in __getattr__(self, key)
96 return self[key]
97 except KeyError:
---> 98 raise AttributeError(key)
99
100 def __setstate__(self, state):
AttributeError: columns
如果您想獲取功能,則不使用列。 你已經在上面定義了
X = diabetes.data
據我所知,內置數據集不包含每列的任何 header 。 你必須自己定義它。 例如使用 pandas 庫
您可以打印出列鍵,而不是通過它的鍵打印任何列
keys= diabetes.keys()
print(keys)
feature_labels = diabetes["data"]
print(feature_labels)
或者如果你想遍歷鍵,你可以做這樣的事情
for key in diabetes.keys():
print(key)
feature_labels = diabetes[key ]
print(feature_labels)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.