python 利用sklearn.cross_validation的KFold构造交叉验证数据集

KFold通过提供index来给你确定不同组的训练集以及测试的index,来构造交叉验证数据集。

参数(n, n_folds=3, shuffle=False, random_state=None)
n为总数
n_folds为分为多少个交叉验证集
shuffle为是否随机
random_state设置随机因子
from sklearn.cross_validation import KFold
import numpy as np
X = np.arange(24).reshape(12,2)
X
array([[ 0,  1],
       [ 2,  3],
       [ 4,  5],
       [ 6,  7],
       [ 8,  9],
       [10, 11],
       [12, 13],
       [14, 15],
       [16, 17],
       [18, 19],
       [20, 21],
       [22, 23]])

1.shuffle=False

kf = KFold(12,n_folds=5,shuffle=False)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
0 [ 3  4  5  6  7  8  9 10 11] [0 1 2]
1 [ 0  1  2  6  7  8  9 10 11] [3 4 5]
2 [ 0  1  2  3  4  5  8  9 10 11] [6 7]
3 [ 0  1  2  3  4  5  6  7 10 11] [8 9]
4 [0 1 2 3 4 5 6 7 8 9] [10 11]
kf = KFold(12,n_folds=5,shuffle=False)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
0 [ 3  4  5  6  7  8  9 10 11] [0 1 2]
1 [ 0  1  2  6  7  8  9 10 11] [3 4 5]
2 [ 0  1  2  3  4  5  8  9 10 11] [6 7]
3 [ 0  1  2  3  4  5  6  7 10 11] [8 9]
4 [0 1 2 3 4 5 6 7 8 9] [10 11]

2.shuffle=True,俩次不同了

kf = KFold(12,n_folds=5,shuffle=True)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
0 [ 0  1  2  3  4  6  7  8 11] [ 5  9 10]
1 [ 0  2  3  4  5  8  9 10 11] [1 6 7]
2 [ 0  1  2  3  4  5  6  7  9 10] [ 8 11]
3 [ 0  1  2  5  6  7  8  9 10 11] [3 4]
4 [ 1  3  4  5  6  7  8  9 10 11] [0 2]
kf = KFold(12,n_folds=5,shuffle=True)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
0 [ 0  3  4  6  7  8  9 10 11] [1 2 5]
1 [ 1  2  5  6  7  8  9 10 11] [0 3 4]
2 [ 0  1  2  3  4  5  8  9 10 11] [6 7]
3 [ 0  1  2  3  4  5  6  7  8 10] [ 9 11]
4 [ 0  1  2  3  4  5  6  7  9 11] [ 8 10]

3.shuffle=True,random_state赋值,俩次又相同了

kf = KFold(12, n_folds=5, shuffle=True, random_state=5)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
0 [ 0  1  3  4  6  8  9 10 11] [2 5 7]
1 [ 0  1  2  3  5  6  7  8 10] [ 4  9 11]
2 [ 0  2  3  4  5  6  7  9 10 11] [1 8]
3 [ 1  2  3  4  5  6  7  8  9 11] [ 0 10]
4 [ 0  1  2  4  5  7  8  9 10 11] [3 6]
kf = KFold(12, n_folds=5, shuffle=True, random_state=5)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
0 [ 0  1  3  4  6  8  9 10 11] [2 5 7]
1 [ 0  1  2  3  5  6  7  8 10] [ 4  9 11]
2 [ 0  2  3  4  5  6  7  9 10 11] [1 8]
3 [ 1  2  3  4  5  6  7  8  9 11] [ 0 10]
4 [ 0  1  2  4  5  7  8  9 10 11] [3 6]
  • 8
    点赞
  • 52
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值