当前位置:   article > 正文

KFold,StratifiedKFold k折交叉切分_kfold 按照类别切分

kfold 按照类别切分

StratifiedKFold用法类似Kfold,但是他是分层采样,确保训练集,测试集中各类别样本的比例与原始数据集中相同。

例子:

import numpy as np
from sklearn.model_selection import KFold,StratifiedKFold

X=np.array([
    [1,2,3,4],
    [11,12,13,14],
    [21,22,23,24],
    [31,32,33,34],
    [41,42,43,44],
    [51,52,53,54],
    [61,62,63,64],
    [71,72,73,74]
])

y=np.array([1,1,0,0,1,1,0,0])
#n_folds这个参数没有,引入的包不同,
floder = KFold(n_splits=4,random_state=0,shuffle=False)
sfolder = StratifiedKFold(n_splits=4,random_state=0,shuffle=False)

for train, test in sfolder.split(X,y):
    print('Train: %s | test: %s' % (train, test))
    print(" ")

for train, test in floder.split(X,y):
    print('Train: %s | test: %s' % (train, test))
    print(" ")
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

结果:

1.
Train: [1 3 4 5 6 7] | test: [0 2]

Train: [0 2 4 5 6 7] | test: [1 3]

Train: [0 1 2 3 5 7] | test: [4 6]

Train: [0 1 2 3 4 6] | test: [5 7]

2.
Train: [2 3 4 5 6 7] | test: [0 1]

Train: [0 1 4 5 6 7] | test: [2 3]

Train: [0 1 2 3 6 7] | test: [4 5]

Train: [0 1 2 3 4 5] | test: [6 7]

分析:可以看到StratifiedKFold 分层采样交叉切分,确保训练集,测试集中各类别样本的比例与原始数据集中相同。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/586271
推荐阅读
  

闽ICP备14008679号