简体   繁体   English

Receiving KeyError: “[Int64Index([ ... dtype='int64', length=1323)] 都不在 [columns]”

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

SUMMARY概括

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?我是否错误地拆分数据或将格式错误的数据发送到我的函数中?

WHAT I'VE TRIED我的尝试

  • Read through several StackOverflow posts with the same KeyError通读多个具有相同 KeyError 的 StackOverflow 帖子
  • Re-ead through scikit-learn example I followed重新阅读我遵循的scikit-learn 示例
  • Reviewed previous versions of my code to troubleshoot查看我的代码的先前版本以进行故障排除

I am running this within a CoLab document and it can be viewed here我在 CoLab 文档中运行它,可以在此处查看

CODE代码

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)

EXPECTED RESULT预期结果

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.

相关问题 KeyError:“[Int64Index dtype='int64', length=9313)] 都不在 [columns]” - KeyError: "None of [Int64Index dtype='int64', length=9313)] are in the [columns]" Python Mlens Ensemble:KeyError:“[Int64Index([... dtype='int64', length=105)] 均不在 [columns] 中” - Python Mlens Ensemble: KeyError: "None of [Int64Index([... dtype='int64', length=105)] are in the [columns]" 读取 CSV & Columns - KeyError: “[Int64Index([0, 1, 2, 3], dtype='int64')] 都在 [columns] 中” - Reading CSV & Columns - KeyError: “None of [Int64Index([0, 1, 2, 3], dtype='int64')] are in the [columns]” KeyError: "[Int64Index([ 12313,\\n, 34534],\\n dtype='int64', leng - KeyError: "None of [Int64Index([ 12313,\n , 34534],\n dtype='int64', leng 关键错误:[Int64Index([…]dtype='int64')] 均不在 [columns] 中 - Key Error: None of [Int64Index([…]dtype='int64')] are in the [columns] 关键错误:[Int64Index…] dtype='int64] 均不在 [columns] 中 - Key Error: None of [Int64Index…] dtype='int64] are in the [columns] Sklearn 错误:[Int64Index([2, 3], dtype='int64')] 均不在 [columns] 中 - Sklearn error: None of [Int64Index([2, 3], dtype='int64')] are in the [columns] [Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64', name='index')] 中没有一个在 [index] - None of [Int64Index([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype='int64', name='index')] are in the [index] KeyError:使用 drop_duplicates 时的 Int64Index([1], dtype='int64') - KeyError: Int64Index([1], dtype='int64') when using drop_duplicates 迭代并更改以熊猫为单位的行的值(错误“ [index]中[Int64Index([10],dtype ='int64')]都不存在”) - Iterating and changing value of the row in pandas ( Error “None of [Int64Index([10], dtype='int64')] are in the [index]” )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM