將全部訓練集分成k個不相交的子集,假設訓練集的訓練樣例個數為m,那麼每一個子集有m/k個訓練樣例,比如[1,2,3,4,5,6]分成兩份,則第一份可能為[1,3,5],第二份[2,4,6]。
每次從分好的子集裡面,拿出一個作為測試集,其他k-1個作為訓練集
在k-1個訓練集上訓練出學習器模型,把這個模型用測試集來驗證,最後求得所有子集的分類率的平均值,作為該模型或者假設函數的真實分類率。
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]