gpt4 book ai didi

python - sklearn 随机状态不是随机的

转载 作者:太空宇宙 更新时间:2023-11-04 00:36:01 25 4
gpt4 key购买 nike

我一直在研究 StratifiedKFold 中的随机状态变量在 sklearn 中,但它似乎不是随机的。我相信设置 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

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

最佳答案

来自链接文档

random_state : None, int or RandomState

When shuffle=True, pseudo-random number generator state used for shuffling. If None, use default numpy RNG for shuffling.

您没有在调用 StratifiedKFold 时设置 shuffle=True,因此 random_state 不会执行任何操作。

关于python - sklearn 随机状态不是随机的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44029465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com