Trying to create a KFold
object for my xgboost.cv
, and I have
import pandas as pd
from sklearn.model_selection import KFold
df = pd.DataFrame([[1,2,3,4,5],[6,7,8,9,10]])
KF = KFold(n_splits=2)
kf = KF.split(df)
But it seems I can only enumerate once:
for i, (train_index, test_index) in enumerate(kf):
print(f"Fold {i}")
for i, (train_index, test_index) in enumerate(kf):
print(f"Again_Fold {i}")
gives output of
Fold 0
Fold 1
The second enumerate seems to be on an empty object.
I am probably fundamentally understanding something wrong, or completed messed up somewhere, but could someone explain this behavior?
[Edit, adding follow up question] This behavior seems to cause passing KFold object to xgboost.cv
setting xgboost.cv(..., folds = KF.split(df))
to have index out of range error. My fix is to recreate the list of tuples with
kf = []
for i, (train_index, test_index) in enumerate(KF.split(df)):
this_split = (list(train_index), list(test_index))
kf.append(this_split)
xgboost.cv(..., folds = kf)
looking for smarter solutions.