Receiving KeyError: “[Int64Index([ ... dtype='int64', length=1323)] 不在 [columns]"中 [英] Receiving KeyError: "None of [Int64Index([ ... dtype='int64', length=1323)] are in the [columns]"

查看:233
本文介绍了Receiving KeyError: “[Int64Index([ ... dtype='int64', length=1323)] 不在 [columns]"中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将测试和训练数据输入 ROC 曲线图时,我收到以下错误:

When feeding test and train data into a ROC curve plot, I receive the following error:

KeyError: [Int64Index([ 0, 1, 2, ... dtype='int64', length=1323)] 都在 [columns] 中"

KeyError: "None of [Int64Index([ 0, 1, 2, ... dtype='int64', length=1323)] are in the [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?

  • 通读多个具有相同 KeyError 的 StackOverflow 帖子
  • 重新阅读scikit-learn 示例 我跟着
  • 查看了我以前版本的代码以进行故障排除

我在 CoLab 文档中运行它,可以在此处查看

I am running this within a CoLab document and it can be viewed here

我正在使用标准数据帧来提取我的 X 和 Y 集:

I am using standard dataframes to pull in my X and Y sets:

X = df_full.drop(['Attrition'], axis=1)
y = df_full['Attrition'].as_matrix()

KeyError 追溯到这里的第 8 行:

The KeyError traces back to the 8th line here:

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)

预期结果

我应该能够将数据 X 和 Y 输入到我的函数中.

EXPECTED RESULT

I should be able to feed the data, X and Y, into my function.

推荐答案

在这段代码中,train, test 是索引数组,当您从 DataFrame 中选择时将其用作列:

in this piece of code train, test are arrays of indices, while you using it as a columns when selection from DataFrame:

for train, test in kf.split(X, Y):
    probas_ = model.fit(X[train], Y[train]).predict_proba(X[test])

你应该使用 iloc 代替:

    probas_ = model.fit(X.iloc[train], Y.iloc[train]).predict_proba(X.iloc[test])

这篇关于Receiving KeyError: “[Int64Index([ ... dtype='int64', length=1323)] 不在 [columns]"中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆