Home > Net >  Split dataset into 5~fold for cross-validation
Split dataset into 5~fold for cross-validation

Time:11-02

I have a dataset that I want to split into 5-fold (distinct), instead of traditional 80-20 split.

So for example:

X = pd.DataFrame({'a': [1, 3, 5, 7, 4, 5, 6, 4, 7, 9],
                  'b': [3, 5, 6, 2, 4, 6, 7, 8, 7, 8],
                  'c': [2, 3, 4, 5, 6, 7, 8, 9, 2, 1]} )
y = [2, 3, 1, 1, 3, 2, 1, 3, 2, 2]

X

    a   b   c
0   1   3   2
1   3   5   3
2   5   6   4
3   7   2   5
4   4   4   6
5   5   6   7
6   6   7   8
7   4   8   9
8   7   7   2
9   9   8   1

So that I have X1,X2,..,X5 with corresponding y1,y2,..,y5.

CodePudding user response:

Use KFold from sklearn:

from sklearn.model_selection import KFold

print(list(kf.split(X, y)))

# Output:
[(array([2, 3, 4, 5, 6, 7, 8, 9]), array([0, 1])),
 (array([0, 1, 4, 5, 6, 7, 8, 9]), array([2, 3])),
 (array([0, 1, 2, 3, 6, 7, 8, 9]), array([4, 5])),
 (array([0, 1, 2, 3, 4, 5, 8, 9]), array([6, 7])),
 (array([0, 1, 2, 3, 4, 5, 6, 7]), array([8, 9]))]
  • Related