sklearn的train_test_split函数的random_state

  我们使用sklearn进行机器学习之前,一般使用train_test_split来进行数据集的分割,其参数random_state代表什么呢?

>>>from sklearn.model_selection import train_test_split

>>> x = [1,2,3,4,5,6,7,8,9,10]
>>> y = [1,2,3,4,5,6,7,8,9,10]
x_train, x_test, y_train, y_test = train_test_split(
...     x, y, test_size=0.3)# 测试集比例为30%, random_state默认为None
>>> x_train, x_test
([7, 8, 3, 1, 9, 5, 2], [10, 6, 4])

#重新分割
>>> x_train, x_test, y_train, y_test = train_test_split(
...		x, y, test_size=0.3)
>>> x_train, x_test
([7, 8, 5, 4, 9, 1, 2], [6, 10, 3])
>>>

  可以看到,random_state默认状态下,两次分割的结果不一样

>>> x_train, x_test, y_train, y_test = train_test_split(
...		x, y, test_size=0.3, random_state=1)
>>> x_train, x_test
([5, 1, 4, 2, 8, 9, 6], [3, 10, 7])
>>> x_train, x_test, y_train, y_test = train_test_split(
...		x, y, test_size=0.3, random_state=1)
>>> x_train, x_test
([5, 1, 4, 2, 8, 9, 6], [3, 10, 7])

  设置了random_state之后,两次分割的结果一样

  结论: random_state的值相当于一种规则,通过设定为相同的数,每次分割的结果都是相同的

  • 13
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值