# What does the KFold error mean and how to get confusion matrix from Kfold random forest implementation?

from sklearn.model_selection import KFold

num_folds = 10

seed = 77

kf = KFold(n_splits=num_folds,random_state=77,shuffle=False)


rfc=RandomForestClassifier(n_jobs=2,n_estimators=500,criterion="entropy",random_state=9999)

kf.get_n_splits(X)

for train_index, test_index in kf.split(X):

X_train, X_test = X[train_index], X[test_index]

y_train, y_test = y[train_index], y[test_index]

rfc.fit(X_train, y_train)

print(confusion_matrix(y_test, rfc.predict(X_test)))

print(10* '#')


I get error which I dont understand. KeyError: "None of [Int64Index([100, 101, 102, 103, 104, 105, 106, 107, 108, 109,\n ...\n 990, 991, 992, 993, 994, 995, 996, 997, 998, 999],\n dtype='int64', length=900)] are in the [columns]"

modified my code like below, works now

kf = KFold(n_splits=num_folds,random_state=seed,shuffle=False)
kf.get_n_splits(X)
i=1
print("confusion matrix:")
for train_index, test_index in kf.split(X):

X_train, X_test = X.iloc[train_index,:], X.iloc[test_index,:]
y_train, y_test = y.iloc[train_index], y.iloc[test_index]

rfc.fit(X_train, y_train)
print (i,"\n",confusion_matrix(y_test, rfc.predict(X_test)))
i=i+1
print(10* '#')