[英]Receiving KeyError: "None of [Int64Index([ ... dtype='int64', length=1323)] are in the [columns]"
When feeding test and train data into a ROC curve plot, I receive the following error:将测试和训练数据输入 ROC 曲线图时,我收到以下错误:
KeyError: "None of [Int64Index([ 0, 1, 2, ... dtype='int64', length=1323)] are in the [columns]" KeyError:“[Int64Index([ 0, 1, 2, ... dtype='int64', length=1323)] 都在 [columns] 中”
The error seems to be saying that it doesn't like the format of my data, but it worked when run the first time and I haven't been able to get it to run again.该错误似乎是说它不喜欢我的数据格式,但它在第一次运行时有效,但我无法再次运行它。
Am I incorrectly splitting my data or sending incorrectly formatted data into my function?我是否错误地拆分数据或将格式错误的数据发送到我的函数中?
I am running this within a CoLab document and it can be viewed here我在 CoLab 文档中运行它,可以在此处查看
I am using standard dataframes to pull in my X and Y sets:我正在使用标准数据帧来拉入我的 X 和 Y 集:
X = df_full.drop(['Attrition'], axis=1)
y = df_full['Attrition'].as_matrix()
The KeyError traces back to the 8th line here: KeyError 可以追溯到这里的第 8 行:
def roc_plot(X, Y, Model):
tprs = []
aucs = []
mean_fpr = np.linspace(0, 1, 100)
plt.figure(figsize=(12,8))
i = 0
for train, test in kf.split(X, Y):
probas_ = model.fit(X[train], Y[train]).predict_proba(X[test])
# Compute ROC curve and area the curve
fpr, tpr, thresholds = roc_curve(Y[test], probas_[:, 1])
tprs.append(np.interp(mean_fpr, fpr, tpr))
tprs[-1][0] = 0.0
roc_auc = auc(fpr, tpr)
aucs.append(roc_auc)
plt.plot(fpr, tpr, lw=1, alpha=0.3,
label='ROC fold %d (AUC = %0.2f)' % (i, roc_auc))
i += 1
plt.plot([0, 1], [0, 1], linestyle='--', lw=2, color='r',
label='Chance', alpha=.8)
mean_tpr = np.mean(tprs, axis=0)
mean_tpr[-1] = 1.0
mean_auc = auc(mean_fpr, mean_tpr)
std_auc = np.std(aucs)
plt.plot(mean_fpr, mean_tpr, color='b',
label=r'Mean ROC (AUC = %0.2f $\pm$ %0.2f)' % (mean_auc, std_auc),
lw=2, alpha=.8)
std_tpr = np.std(tprs, axis=0)
tprs_upper = np.minimum(mean_tpr + std_tpr, 1)
tprs_lower = np.maximum(mean_tpr - std_tpr, 0)
plt.fill_between(mean_fpr, tprs_lower, tprs_upper, color='grey', alpha=.2,
label=r'$\pm$ 1 std. dev.')
plt.xlim([-0.05, 1.05])
plt.ylim([-0.05, 1.05])
plt.xlabel('False Positive Rate')
plt.ylabel('True Positive Rate')
plt.title('Receiver operating characteristic example')
plt.legend(loc="lower right")
plt.show()
It happens when I run the following with the function:当我使用该函数运行以下命令时会发生这种情况:
model = XGBClassifier() # Create the Model
roc_plot(X, Y, Model)
I should be able to feed the data, X and Y, into my function.我应该能够将数据 X 和 Y 输入到我的函数中。
in this piece of code train, test
are arrays of indices, while you using it as a columns when selection from DataFrame:在这段代码中train, test
是索引数组,而从 DataFrame 中选择时将其用作列:
for train, test in kf.split(X, Y):
probas_ = model.fit(X[train], Y[train]).predict_proba(X[test])
you should use iloc
instead:你应该使用iloc
代替:
probas_ = model.fit(X.iloc[train], Y.iloc[train]).predict_proba(X.iloc[test])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.