python实现 K折交叉验证/hold out

holdout

将训练集分为train,validation 测试集保持不变
holdout缺点:模型性能评估对训练集的划分极其敏感

k折交叉验证

sklearn KFold

from sklearn.model_selection import KFold
import numpy as np

kf = KFold(n_splits =3,shuffle= True ) #n_splits > =2  random_state=np.random.seed(1)可保证每次随机都一样
print kf
x = np.array([x for x in range(15)])
print x 
for train_index, test_index in kf.split(x):
	print train_index,test_index
# 结果
KFold(n_splits=3, random_state=None, shuffle=True)
[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]

[ 0  1  3  4  5  7  8  9 10 11] [ 2  6 12 13 14]
[ 0  2  5  6  7 10 11 12 13 14] [1 3 4 8 9]
[ 1  2  3  4  6  8  9 12 13 14] [ 0  5  7 10 11]

至于holdout,从Kfold中随便取一个就好了

import sklearn

参考

如何在kaggle首战中进入前10%

你可能感兴趣的:(machine,learning)