StratifiedKFold和KFold(5折验证)交叉验证的联系和区别Python实例

Kfold:

将全部训练集分成k个不相交的子集,假设训练集的训练样例个数为m,那么每一个子集有m/k个训练样例,比如[1,2,3,4,5,6]分成两份,则第一份可能为[1,3,5],第二份[2,4,6]。
每次从分好的子集里面,拿出一个作为测试集,其他k-1个作为训练集
在k-1个训练集上训练出学习器模型,把这个模型用测试集来验证,最后求得所有子集的分类率的平均值,作为该模型或者假设函数的真实分类率。

StratifiedKFold

StratifiedKFold用法类似Kfold,但是他是分层采样,确保训练集的预测结果(0,1)都占有,测试集中各类别样本的比例与原始数据集中相同。也就是正负例都含有。

例子:

import numpy as np
from sklearn.model_selection import StratifiedKFold,KFold
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('StratifiedKFold Train index: %s | test: %s' % (train, test))
    print('X[train]:',X[train])
    print('y[train]:',y[train])
    print('X[test]:',X[test])
    print('y[test]:',y[test])
    print(" ")

for train, test in floder.split(X,y):
    print('KFold Train index: %s | test index : %s' % (train, test))
    print('X[train]:', X[train])
    print('y[train]:', y[train])
    print('X[test]:', X[test])
    print('y[test]:', y[test])
    print(" ")

结果:

D:\ProgramFiles\Anaconda3\python.exe "D:/Python Project/Finance-Cup-Data-master/Data-Finance-Cup/luojiLearn/KfoldLearn.py"
StratifiedKFold Train: [1 3 4 5 6 7] | test: [0 2]
X[train]: [[11 12 13 14]
 [31 32 33 34]
 [41 42 43 44]
 [51 52 53 54]
 [61 62 63 64]
 [71 72 73 74]]
y[train]: [1 0 1 1 0 0]
X[test]: [[ 1  2  3  4]
 [21 22 23 24]]
y[test]: [1 0]
 
StratifiedKFold Train: [0 2 4 5 6 7] | test: [1 3]
X[train]: [[ 1  2  3  4]
 [21 22 23 24]
 [41 42 43 44]
 [51 52 53 54]
 [61 62 63 64]
 [71 72 73 74]]
y[train]: [1 0 1 1 0 0]
X[test]: [[11 12 13 14]
 [31 32 33 34]]
y[test]: [1 0]
 
StratifiedKFold Train: [0 1 2 3 5 7] | test: [4 6]
X[train]: [[ 1  2  3  4]
 [11 12 13 14]
 [21 22 23 24]
 [31 32 33 34]
 [51 52 53 54]
 [71 72 73 74]]
y[train]: [1 1 0 0 1 0]
X[test]: [[41 42 43 44]
 [61 62 63 64]]
y[test]: [1 0]
 
StratifiedKFold Train: [0 1 2 3 4 6] | test: [5 7]
X[train]: [[ 1  2  3  4]
 [11 12 13 14]
 [21 22 23 24]
 [31 32 33 34]
 [41 42 43 44]
 [61 62 63 64]]
y[train]: [1 1 0 0 1 0]
X[test]: [[51 52 53 54]
 [71 72 73 74]]
y[test]: [1 0]
 
KFold Train: [2 3 4 5 6 7] | test: [0 1]
X[train]: [[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[train]: [0 0 1 1 0 0]
X[test]: [[ 1  2  3  4]
 [11 12 13 14]]
y[test]: [1 1]
 
KFold Train: [0 1 4 5 6 7] | test: [2 3]
X[train]: [[ 1  2  3  4]
 [11 12 13 14]
 [41 42 43 44]
 [51 52 53 54]
 [61 62 63 64]
 [71 72 73 74]]
y[train]: [1 1 1 1 0 0]
X[test]: [[21 22 23 24]
 [31 32 33 34]]
y[test]: [0 0]
 
KFold Train: [0 1 2 3 6 7] | test: [4 5]
X[train]: [[ 1  2  3  4]
 [11 12 13 14]
 [21 22 23 24]
 [31 32 33 34]
 [61 62 63 64]
 [71 72 73 74]]
y[train]: [1 1 0 0 0 0]
X[test]: [[41 42 43 44]
 [51 52 53 54]]
y[test]: [1 1]
 
KFold Train: [0 1 2 3 4 5] | test: [6 7]
X[train]: [[ 1  2  3  4]
 [11 12 13 14]
 [21 22 23 24]
 [31 32 33 34]
 [41 42 43 44]
 [51 52 53 54]]
y[train]: [1 1 0 0 1 1]
X[test]: [[61 62 63 64]
 [71 72 73 74]]
y[test]: [0 0]

你可能感兴趣的:(机器学习,机器学习,python)