前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >sklearn cross validation_python sklearn

sklearn cross validation_python sklearn

作者头像
全栈程序员站长
发布2022-09-27 14:40:53
3280
发布2022-09-27 14:40:53
举报
文章被收录于专栏:全栈程序员必看

大家好,又见面了,我是你们的朋友全栈君。

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

代码语言:javascript
复制
参数(n, n_folds=3, shuffle=False, random_state=None)
n为总数
n_folds为分为多少个交叉验证集
shuffle为是否随机
random_state设置随机因子
代码语言:javascript
复制
from sklearn.cross_validation import KFold
代码语言:javascript
复制
import numpy as np
代码语言:javascript
复制
X = np.arange(24).reshape(12,2)
代码语言:javascript
复制
X
代码语言:javascript
复制
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

代码语言:javascript
复制
kf = KFold(12,n_folds=5,shuffle=False)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
代码语言:javascript
复制
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]
代码语言:javascript
复制
kf = KFold(12,n_folds=5,shuffle=False)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
代码语言:javascript
复制
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,俩次不同了

代码语言:javascript
复制
kf = KFold(12,n_folds=5,shuffle=True)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
代码语言:javascript
复制
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]
代码语言:javascript
复制
kf = KFold(12,n_folds=5,shuffle=True)
for i,(train_index,test_index) in enumerate(kf):
    print(i,train_index,test_index)
代码语言:javascript
复制
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赋值,俩次又相同了

代码语言:javascript
复制
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)
代码语言:javascript
复制
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]
代码语言:javascript
复制
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)
代码语言:javascript
复制
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]

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/193297.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • KFold通过提供index来给你确定不同组的训练集以及测试的index,来构造交叉验证数据集。
    • 1.shuffle=False
      • 2.shuffle=True,俩次不同了
        • 3.shuffle=True,random_state赋值,俩次又相同了
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档