random_state参数

KFold(n_splits=5, shuffle=False, random_state=None)

该函数用来做K折交叉验证。

n_splits:折数,int型,默认值为5.

shuffle:对数据进行划分前是否进行洗牌。boolean型

random_state:int, RandomState instance 或 None, 默认为None。直译为“随机状态”。

只有当shuffle=True时,random_state才有意义。

当random_state=None时,代表每次数据的划分不一样

可以看到,两次的数据划分不一样,每次划分前都重新洗牌一次

import pandas as pd
import numpy as np
from sklearn.model_selection import KFold
 
xx=np.arange(25)
kf=KFold(n_splits=5,shuffle=True,random_state=None)
for train_index,test_index in kf.split(xx):
    print('train_index:%s,test_index:%s'%(train_index,test_index))
#输出结果
train_index:[ 0  1  2  3  4  5  6  8  9 10 11 12 14 15 17 18 19 20 21 23],test_index:[ 7 13 16 22 24]
train_index:[ 0  2  4  5  6  7  8 10 11 12 13 14 15 16 17 19 20 21 22 24],test_index:[ 1  3  9 18 23]
train_index:[ 1  2  3  5  7  8  9 10 11 12 13 14 15 16 18 20 21 22 23 24],test_index:[ 0  4  6 17 19]
train_index:[ 0  1  2  3  4  6  7  9 11 12 13 16 17 18 19 20 21 22 23 24],test_index:[ 5  8 10 14 15]
train_index:[ 0  1  3  4  5  6  7  8  9 10 13 14 15 16 17 18 19 22 23 24],test_index:[ 2 11 12 20 21]
#重新运行一次
kf=KFold(n_splits=5,shuffle=True,random_state=None)
for train_index,test_index in kf.split(xx):
    print('train_index:%s,test_index:%s'%(train_index,test_index))
# 输出结果
train_index:[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 17 18 19 24],test_index:[16 20 21 22 23]
train_index:[ 1  2  3  4  5  6  7  8  9 11 12 14 15 16 17 20 21 22 23 24],test_index:[ 0 10 13 18 19]
train_index:[ 0  2  4  5  6  7  9 10 11 12 13 14 15 16 18 19 20 21 22 23],test_index:[ 1  3  8 17 24]
train_index:[ 0  1  3  7  8  9 10 12 13 14 15 16 17 18 19 20 21 22 23 24],test_index:[ 2  4  5  6 11]
train_index:[ 0  1  2  3  4  5  6  8 10 11 13 16 17 18 19 20 21 22 23 24],test_index:[ 7  9 12 14 15]

而当为random_state指定一个整数时。

例如指定为“1”,

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值