赞
踩
StratifiedKFold用法类似Kfold,但是他是分层采样,确保训练集,测试集中各类别样本的比例与原始数据集中相同
Parameters
Number of folds. Must be at least 2.
Whether to shuffle each stratification of the data before splitting into batches.
int, RandomState instance or None, optional, default=None
If int, random_state is the seed used by the random number generatorIf RandomState instance, random_state is the random number generator;
If None, the random number generator is the RandomState instance used
by `np.random`. Used when ``shuffle`` == True.
- 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])
-
- sfolder=StratifiedKFold(n_splits=4,random_state=0,shuffle=False)
- floder = KFold(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(" ")

StratifiedKFold KFold
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。