sklearn kfold在python中返回错误的索引

Hil*_*laD 6 python scikit-learn

我在python上的sklearn包中使用kfold函数在df(数据框)上使用不连续的行索引.

这是代码:

kFold = KFold(n_splits=10, shuffle=True, random_state=None)
for train_index, test_index in kFold.split(dfNARemove):...
Run Code Online (Sandbox Code Playgroud)

我得到了一些在我的df中不存在的train_index或test_index.

我能做什么?

Edu*_*sov 12

kFold迭代器为您提供DataFrame的训练和验证对象的位置索引,而不是它们的非连续索引.您可以使用.ilocpandas方法访问您的火车和验证对象:

kFold = KFold(n_splits=10, shuffle=True, random_state=None)
for train_index, test_index in kFold.split(dfNARemove):
    train_data = dfNARemove.iloc[train_index]
    test_data = dfNARemove.iloc[test_index]
Run Code Online (Sandbox Code Playgroud)

如果您想知道每个折叠上用于train_index和test_index的非连续索引,您可以执行以下操作:

non_continuous_train_index = dfNARemove.index[train_index]
non_continuous_test_index = dfNARemove.index[test_index]
Run Code Online (Sandbox Code Playgroud)