我们在用python做机器学习的交叉验证工作时,常会遇到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时:

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

代表每次数据的划分不一样。

举例:

import pandas as pd
import numpy as np
from sklearn.model_selection import KFoldxx=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”:

KFold(n_splits=5, shuffle=True, random_state=1)

代表每次数据的划分一样。

举例:

xx=np.arange(25)
kf=KFold(n_splits=5,shuffle=True,random_state=1)
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  4  5  6  7  8  9 10 11 12 15 16 18 19 20 22 23 24],test_index:[ 3 13 14 17 21]
train_index:[ 0  1  3  5  6  7  8  9 11 12 13 14 15 16 17 20 21 22 23 24],test_index:[ 2  4 10 18 19]
train_index:[ 0  2  3  4  5  8  9 10 11 12 13 14 15 16 17 18 19 21 23 24],test_index:[ 1  6  7 20 22]
train_index:[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 17 18 19 20 21 22],test_index:[ 0 15 16 23 24]
train_index:[ 0  1  2  3  4  6  7 10 13 14 15 16 17 18 19 20 21 22 23 24],test_index:[ 5  8  9 11 12]

再重新运行一次:

kf=KFold(n_splits=5,shuffle=True,random_state=1)
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  4  5  6  7  8  9 10 11 12 15 16 18 19 20 22 23 24],test_index:[ 3 13 14 17 21]
train_index:[ 0  1  3  5  6  7  8  9 11 12 13 14 15 16 17 20 21 22 23 24],test_index:[ 2  4 10 18 19]
train_index:[ 0  2  3  4  5  8  9 10 11 12 13 14 15 16 17 18 19 21 23 24],test_index:[ 1  6  7 20 22]
train_index:[ 1  2  3  4  5  6  7  8  9 10 11 12 13 14 17 18 19 20 21 22],test_index:[ 0 15 16 23 24]
train_index:[ 0  1  2  3  4  6  7 10 13 14 15 16 17 18 19 20 21 22 23 24],test_index:[ 5  8  9 11 12]

可见,当两次都将random_state指定为整数“1”时,两次洗牌的结果一样,数据的划分结果一样。

那么问题来了,我让random_state=2行不行?random_state=160行不行?random_state=1与random_state=2有什么区别?看到有个博文设定random_state=42,我就懵×了……:

这个42代表什么含义?

好吧,那我们研究一下:

我们设定random_state=2,运行一下试试:

kf=KFold(n_splits=5,shuffle=True,random_state=2)
for train_index,test_index in kf.split(xx):print('train_index:%s,test_index:%s'%(train_index,test_index))

输出结果:

train_index:[ 1  2  3  4  5  7  8  9 10 11 12 13 15 16 18 19 20 21 22 24],test_index:[ 0  6 14 17 23]
train_index:[ 0  1  2  4  5  6  7  8 10 11 13 14 15 17 18 19 20 21 23 24],test_index:[ 3  9 12 16 22]
train_index:[ 0  2  3  6  7  8  9 11 12 13 14 15 16 17 18 20 21 22 23 24],test_index:[ 1  4  5 10 19]
train_index:[ 0  1  3  4  5  6  8  9 10 11 12 13 14 15 16 17 19 22 23 24],test_index:[ 2  7 18 20 21]
train_index:[ 0  1  2  3  4  5  6  7  9 10 12 14 16 17 18 19 20 21 22 23],test_index:[ 8 11 13 15 24]

还保持random_state=2,再运行一下试试:

kf=KFold(n_splits=5,shuffle=True,random_state=2)
for train_index,test_index in kf.split(xx):print('train_index:%s,test_index:%s'%(train_index,test_index))

输出结果:

train_index:[ 1  2  3  4  5  7  8  9 10 11 12 13 15 16 18 19 20 21 22 24],test_index:[ 0  6 14 17 23]
train_index:[ 0  1  2  4  5  6  7  8 10 11 13 14 15 17 18 19 20 21 23 24],test_index:[ 3  9 12 16 22]
train_index:[ 0  2  3  6  7  8  9 11 12 13 14 15 16 17 18 20 21 22 23 24],test_index:[ 1  4  5 10 19]
train_index:[ 0  1  3  4  5  6  8  9 10 11 12 13 14 15 16 17 19 22 23 24],test_index:[ 2  7 18 20 21]
train_index:[ 0  1  2  3  4  5  6  7  9 10 12 14 16 17 18 19 20 21 22 23],test_index:[ 8 11 13 15 24]

看到了吗,两次random_state=2对数据的划分结果一致,两次random_state=1对数据的划分结果一致,random_state=1与random_state=2的数据划分结果不一致。

因此,赋予random_state的整数,不代表数值意义,而只是一种编号,1、2、42或者160都只是一种编号,不代表具体的数值,不分谁大谁小,只代表一种洗牌结果。

参考资料:

  1. https://blog.csdn.net/ytomc/article/details/113437926
  2. https://www.imooc.com/article/317856

附录:

附录里分别跑了两次random_state=42与random_state=160的运行结果,献给强迫症读者:

第一次跑random_state=42:

kf=KFold(n_splits=5,shuffle=True,random_state=42)
for train_index,test_index in kf.split(xx):print('train_index:%s,test_index:%s'%(train_index,test_index))

运行结果:

train_index:[ 1  2  3  4  5  6  7  9 10 12 13 14 15 17 18 19 20 21 22 24],test_index:[ 0  8 11 16 23]
train_index:[ 0  2  3  4  6  7  8 10 11 12 14 15 16 17 18 19 20 21 23 24],test_index:[ 1  5  9 13 22]
train_index:[ 0  1  5  6  7  8  9 10 11 13 14 16 17 18 19 20 21 22 23 24],test_index:[ 2  3  4 12 15]
train_index:[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 19 22 23],test_index:[17 18 20 21 24]
train_index:[ 0  1  2  3  4  5  8  9 11 12 13 15 16 17 18 20 21 22 23 24],test_index:[ 6  7 10 14 19]

第二次跑random_state=42:

kf=KFold(n_splits=5,shuffle=True,random_state=42)
for train_index,test_index in kf.split(xx):print('train_index:%s,test_index:%s'%(train_index,test_index))

运行结果:

train_index:[ 1  2  3  4  5  6  7  9 10 12 13 14 15 17 18 19 20 21 22 24],test_index:[ 0  8 11 16 23]
train_index:[ 0  2  3  4  6  7  8 10 11 12 14 15 16 17 18 19 20 21 23 24],test_index:[ 1  5  9 13 22]
train_index:[ 0  1  5  6  7  8  9 10 11 13 14 16 17 18 19 20 21 22 23 24],test_index:[ 2  3  4 12 15]
train_index:[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 19 22 23],test_index:[17 18 20 21 24]
train_index:[ 0  1  2  3  4  5  8  9 11 12 13 15 16 17 18 20 21 22 23 24],test_index:[ 6  7 10 14 19]

第一次跑random_state=160:

kf=KFold(n_splits=5,shuffle=True,random_state=160)
for train_index,test_index in kf.split(xx):print('train_index:%s,test_index:%s'%(train_index,test_index))

运行结果:

train_index:[ 1  2  3  4  5  7  8  9 10 11 12 14 15 16 17 19 20 22 23 24],test_index:[ 0  6 13 18 21]
train_index:[ 0  1  2  3  4  5  6  7  8 11 12 13 15 16 18 19 21 22 23 24],test_index:[ 9 10 14 17 20]
train_index:[ 0  1  2  6  8  9 10 12 13 14 15 16 17 18 19 20 21 22 23 24],test_index:[ 3  4  5  7 11]
train_index:[ 0  1  3  4  5  6  7  8  9 10 11 13 14 15 17 18 20 21 22 24],test_index:[ 2 12 16 19 23]
train_index:[ 0  2  3  4  5  6  7  9 10 11 12 13 14 16 17 18 19 20 21 23],test_index:[ 1  8 15 22 24]

第二次跑random_state=160:

kf=KFold(n_splits=5,shuffle=True,random_state=160)
for train_index,test_index in kf.split(xx):print('train_index:%s,test_index:%s'%(train_index,test_index))

运行结果:

train_index:[ 1  2  3  4  5  7  8  9 10 11 12 14 15 16 17 19 20 22 23 24],test_index:[ 0  6 13 18 21]
train_index:[ 0  1  2  3  4  5  6  7  8 11 12 13 15 16 18 19 21 22 23 24],test_index:[ 9 10 14 17 20]
train_index:[ 0  1  2  6  8  9 10 12 13 14 15 16 17 18 19 20 21 22 23 24],test_index:[ 3  4  5  7 11]
train_index:[ 0  1  3  4  5  6  7  8  9 10 11 13 14 15 17 18 20 21 22 24],test_index:[ 2 12 16 19 23]
train_index:[ 0  2  3  4  5  6  7  9 10 11 12 13 14 16 17 18 19 20 21 23],test_index:[ 1  8 15 22 24]

看到了吧,random_state为同一数值时,数据划分结果就一样,random_state为不同数值时,数据划分结果就不一样。不管是1、2、42还是160

回答“关于用python做机器学习工作中的random_state参数到底是个什么意思”相关推荐

  1. python在日常工作中的具体应用_python在实际工作中的应用有哪些

    python在实际工作的应用程序是什么无论是工作还是生活,python是一种实用和有趣的技能.Python已经广泛应用,下面的图可以概括:从工作:Python开发,Python爬虫,大数据;从生活中, ...

  2. Python 在数据分析工作中的地位与 R 语言比较如何?

    随着数据分析在各个行业的广泛应用,数据分析工具的选择变得越来越重要.那么Python 在数据分析工作中的地位与 R 语言.SAS.SPSS 比较如何呢? Python和R语言成为了两个最受欢迎的工具. ...

  3. 机器学习中的random_state参数

    作用:控制随机状态. 原因:为什么需要用到这样一个参数random_state(随机状态)? 在此先简单罗列三种情况: 1.在构建模型时: forest = RandomForestClassifie ...

  4. python 在日常工作中_每天在工作和生活中做的事,我如何用 Python 让它们变得更轻松?...

    原标题:每天在工作和生活中做的事,我如何用 Python 让它们变得更轻松? 在生活和工作中,往往充斥着一些枯燥且乏味的事情.所以要让自己从机械地重复性劳动中解放出来,才是解放身心的正确姿势. 本文列 ...

  5. 0基础学python做什么工作好-零基础学Python,越早明白这些,越快找到好工作!...

    如何学习python,就能仅靠python得到好工作? 读者A:自学python一年,用于写网络爬虫,但一直觉得自己对python理解还很低层.现在大三,打算再用一年时间学习python,让自己对这们 ...

  6. 0基础学python做什么工作好-如何快速学习Python编程?可以做什么职业?

    近些年Python热度不减,一直占据编程语言排行榜的前几位.人生苦短,让我们来学一学Pyhotn吧,找到一份满意的工作还是没有问题的. Python语言本身所具有的优势,决定了从事Python学习的开 ...

  7. python在会计工作中的应用-python有什么用(会计专业)

    python有什么用 python作为一门高档编程言语,它bai的诞生尽管du很偶尔,但是它得到zhi程序员的喜欢却是必然之dao路,Python入门简略,比较于其他言语,初学者很容易入门,除此之外, ...

  8. python在会计工作中的应用-浅谈各行各业到底该如何应用python?

    文|一本正经胡说八道的猫 一.[会计.财务] 会计财务都离不开和数据打交道,而且每天有大量重复性工作,所以学习Python最大的用处是:释放大部分人力对数据的处理,解放重复劳动性工作. 另外也在评估预 ...

  9. python在日常工作处理中的应用-记 Python 在实际工作中的第一次应用

    今儿接到一个任务处理上线脚本,在明确了要求之后,寻思再三,为了以后能循环使用,这次得进行脚本化才好,于是就开始了我的拙劣码风. 画风其实是这样的. 有一个文件里面有些数据,需要遍历出每行,然后分别取到 ...

最新文章

  1. Ionic APP 热更新
  2. 程序集信息设置.net
  3. 批量添加DNS的A记录和PTR记录
  4. magento常用的语句
  5. MySQL数据库概述
  6. 【meitong】Windows系统受损切莫将就使用 如何快速修复
  7. java实现ssdp协议_SSDP协议最简单的Reactor / Netty实现是什么?
  8. typecho图片插件_Typecho图片表情插件Smilies1.1.3更新
  9. 转:如何在艰难时期留住好员工
  10. icon、png网页开发中所需要的小图标
  11. 深信服 一面 2018 秋招
  12. SSD算法原理与代码(三)
  13. html 水印插件,jquery图片水印插件
  14. 《极限逃亡》12.3正式上线链游玩家|末日围城、文明重启
  15. 新1期_012课_内存控制器与SDRAM
  16. XCTF-PWN pwn-200(使用DynELF利用write泄露system地址)
  17. 查询销量最高的产品mysql_MYSQL之——查询练习题
  18. Blender 3.2自由定制做体积云絮形状(cloud制作)
  19. 上帝,马加爵,谭卓 等人在天堂对话
  20. moment常用时间操作

热门文章

  1. 奇遇MIX体验:加入全彩VST透视,开创消费级VR一体机新时代
  2. python_paramiko模块用法
  3. 苹果手机找回ID及密码经验
  4. Face Attention Network: An Effective Face Detector for the Occluded Faces
  5. 计算机两个屏幕如何连接不上去,电脑连接两台显示器怎么设置?电脑双屏显示的设置步骤...
  6. AutoLayout -Masonry
  7. oracle数据库ogg延迟,oracle goldengate ogg 源段传输进程lag延迟不断增加的原因?
  8. 中国超级计算行业发展动态及投资战略研究报告(2022-2027年版)
  9. 关于 RTOS 的选择
  10. 深入理解什么是LSM-Tree