我运行以下代码:
np.random.RandomState(3)
idx1 = np.random.choice(range(20),(5,))
idx2 = np.random.choice(range(20),(5,))
np.random.RandomState(3)
idx1S = np.random.choice(range(20),(5,))
idx2S = np.random.choice(range(20),(5,))
我得到的输出如下:
idx1: array([ 2, 19, 19, 9, 4])
idx1S: array([ 2, 19, 19, 9, 4])
idx2: array([ 9, 2, 7, 10, 6])
idx2S: array([ 5, 16, 9, 11, 15])
idx1和idx1S匹配,但idx2和idx2S不匹配。我希望一旦给随机数生成器设定种子并重复相同的命令序列,它就会产生相同的随机数序列。这不是真的吗 还是我还缺少其他东西?
你混淆RandomState
使用seed
。第一行构造了一个对象,您可以将其用作随机源。例如,我们使
>>> rnd = np.random.RandomState(3)
>>> rnd
<mtrand.RandomState object at 0xb17e18cc>
接着
>>> rnd.choice(range(20), (5,))
array([10, 3, 8, 0, 19])
>>> rnd.choice(range(20), (5,))
array([10, 11, 9, 10, 6])
>>> rnd = np.random.RandomState(3)
>>> rnd.choice(range(20), (5,))
array([10, 3, 8, 0, 19])
>>> rnd.choice(range(20), (5,))
array([10, 11, 9, 10, 6])
[我不明白为什么你idx1
和idx1S
同意-但你实际上并没有发布一个自包含的成绩单,所以我怀疑用户错误。]
如果要影响全局状态,请使用seed
:
>>> np.random.seed(3)
>>> np.random.choice(range(20),(5,))
array([10, 3, 8, 0, 19])
>>> np.random.choice(range(20),(5,))
array([10, 11, 9, 10, 6])
>>> np.random.seed(3)
>>> np.random.choice(range(20),(5,))
array([10, 3, 8, 0, 19])
>>> np.random.choice(range(20),(5,))
array([10, 11, 9, 10, 6])
RandomState
起初,使用特定对象似乎不太方便,但是当您需要调整不同的熵流时,使用它会使很多事情变得容易。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句