【问题标题】:sklearn random state not randomsklearn 随机状态不是随机的
【发布时间】:2017-10-17 04:31:19
【问题描述】:

我一直在玩 sklearn 中 StratifiedKFold 的随机状态变量,但它似乎不是随机的。我相信设置random_state=5,应该给我一个不同的测试集,然后设置random_state=4,但情况似乎并非如此。我在下面创建了一些粗略的可重现代码。首先我加载我的数据:

import numpy as np
from sklearn.cross_validation import StratifiedKFold
from sklearn import datasets
iris = datasets.load_iris()
X = iris.data
y = iris.target

然后我设置random_state=5,为此我存储最后的值:

skf=StratifiedKFold(n_splits=5,random_state=5)
for (train, test) in skf.split(X,y): full_test_1=test
full_test_1

array([ 40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  90,  91,  92,
        93,  94,  95,  96,  97,  98,  99, 140, 141, 142, 143, 144, 145,
       146, 147, 148, 149])

random_state=4 执行相同的过程:

skf=StratifiedKFold(n_splits=5,random_state=4)
for (train, test) in skf.split(X,y): full_test_2=test
full_test_2

array([ 40,  41,  42,  43,  44,  45,  46,  47,  48,  49,  90,  91,  92,
        93,  94,  95,  96,  97,  98,  99, 140, 141, 142, 143, 144, 145,
       146, 147, 148, 149])

然后我可以检查它们是否相等:

np.array_equal(full_test_1,full_test_2)
True

我不认为这两个随机状态应该返回相同的数字。我的逻辑或代码有缺陷吗?

【问题讨论】:

    标签: python random scikit-learn cross-validation


    【解决方案1】:

    来自链接的文档

    random_state : 无、int 或 RandomState

    当 shuffle=True 时,用于洗牌的伪随机数生成器状态。如果没有,则使用默认的 numpy RNG 进行洗牌。

    您没有在调用 StratifiedKFold 时设置 shuffle=True,因此 random_state 不会做任何事情。

    【讨论】:

      猜你喜欢
      • 2019-04-09
      • 2015-11-16
      • 2012-10-04
      • 2015-03-19
      • 2014-03-12
      • 2021-03-25
      • 1970-01-01
      相关资源
      最近更新 更多