赞
踩
KFold和StratifiedKFold的作用都是用于配合交叉验证的需求,将数据分割成训练集和测试集。
KFold随机分割数据,不会考虑数据的分布情况。
StratifiedKFold会根据原始数据的分布情况,分割出同分布的数据。
from sklearn.model_selection import KFold from sklearn.model_selection import StratifiedKFold import numpy as np X = np.array([ [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], [81,82,83,84] ]) y = np.array([1,1,0,0,0,1,1,0]) KFold = KFold(n_splits=2, shuffle=True) sKFold = StratifiedKFold(n_splits=2, shuffle=True) print("===KFold split===") for train, test in KFold.split(X, y): print('train index:\n',train) print('train X value:\n',X[train]) print('train y value:\n',y[train]) print('test i
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。