np.random.seed做什么?

np.random.seed(0)

当前回答

它使随机数可预测。 它们都以相同的组合开始,之后的每一次迭代都是相同的。 例子:

Output A: 0, 1, 2
Output B: 1, 3, 5
Output C: 2, 4, 6
Reset seed to 0
Output A: 0, 1, 2
Output B: 1, 3, 5
Output C: 2, 4, 6
Reset seed to 0
Output A: 0, 1, 2
Reset seed to 0
Output A: 0, 1, 2
.
.
.

我希望这对你有所帮助!

其他回答

Numpy文档中有一个很好的解释: https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.random.RandomState.html 它指的是梅森扭扭伪随机数发生器。关于算法的更多细节,请访问:https://en.wikipedia.org/wiki/Mersenne_Twister

Np.random.seed(0)使随机数可预测

>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55,  0.72,  0.6 ,  0.54])
>>> numpy.random.seed(0) ; numpy.random.rand(4)
array([ 0.55,  0.72,  0.6 ,  0.54])

随着种子重置(每次),相同的一组数字将每次出现。

如果随机种子没有被重置,每次调用都会出现不同的数字:

>>> numpy.random.rand(4)
array([ 0.42,  0.65,  0.44,  0.89])
>>> numpy.random.rand(4)
array([ 0.96,  0.38,  0.79,  0.53])

(伪)随机数的工作原理是从一个数字(种子)开始,乘以一个大数,加上一个偏移量,然后对这个和取模。然后,生成的数字被用作生成下一个“随机”数字的种子。当你(每次)设置种子时,它每次都做同样的事情,给你相同的数字。

如果你想要看似随机的数字,不要设置种子。但是,如果您的代码使用了想要调试的随机数,那么在每次运行之前设置种子会非常有帮助,这样代码每次运行时都会执行相同的操作。

要为每次运行获取最多的随机数,请调用numpy.random.seed()。这将导致numpy将种子设置为从/dev/urandom或其Windows模拟程序获得的随机数,或者,如果两者都不可用,它将使用时钟。

有关使用种子生成伪随机数的更多信息,请参阅维基百科。

numpy.random.seed(0)
numpy.random.randint(10, size=5)

这将产生以下输出: 数组([5,0,3,3,7]) 同样,如果我们运行相同的代码,我们将得到相同的结果。

现在,如果我们将种子值0改为1或其他:

numpy.random.seed(1)
numpy.random.randint(10, size=5)

这将产生以下输出:array([5 8 9 5 0]),但现在的输出与上面不同。

如前所述,numpy.random.seed(0)将随机种子设置为0,因此从random获得的伪随机数将从同一点开始。在某些情况下,这有助于调试。然而,经过一些阅读,如果您有线程,这似乎是错误的方法,因为它不是线程安全的。

从differences-between-numpy-random-and-random-random-in-python:

For numpy.random.seed(), the main difficulty is that it is not thread-safe - that is, it's not safe to use if you have many different threads of execution, because it's not guaranteed to work if two different threads are executing the function at the same time. If you're not using threads, and if you can reasonably expect that you won't need to rewrite your program this way in the future, numpy.random.seed() should be fine for testing purposes. If there's any reason to suspect that you may need threads in the future, it's much safer in the long run to do as suggested, and to make a local instance of the numpy.random.Random class. As far as I can tell, random.random.seed() is thread-safe (or at least, I haven't found any evidence to the contrary).

如何做到这一点的例子:

from numpy.random import RandomState
prng = RandomState()
print prng.permutation(10)
prng = RandomState()
print prng.permutation(10)
prng = RandomState(42)
print prng.permutation(10)
prng = RandomState(42)
print prng.permutation(10)

可能给:

[3 0 4 6 8 2 1 9 7 5] [1 6 9 0 2 7 8 3 5 4] [8 1 5 0 7 2 9 4 3 6] [8 1 5 0 7 2 9 4 3 6]

最后,请注意,由于xor的工作方式,在某些情况下初始化为0(而不是所有位都为0的种子)可能会导致一些第一次迭代的不均匀分布,但这取决于算法,超出了我目前的担忧和这个问题的范围。

如果你每次调用numpy的其他随机函数时都设置np.random.seed(a_fixed_number),结果将是相同的:

>>> import numpy as np
>>> np.random.seed(0) 
>>> perm = np.random.permutation(10) 
>>> print perm 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10) 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10) 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10) 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.rand(4) 
[0.5488135  0.71518937 0.60276338 0.54488318]
>>> np.random.seed(0) 
>>> print np.random.rand(4) 
[0.5488135  0.71518937 0.60276338 0.54488318]

然而,如果你只调用它一次,并使用各种随机函数,结果仍然会不同:

>>> import numpy as np
>>> np.random.seed(0) 
>>> perm = np.random.permutation(10)
>>> print perm 
[2 8 4 9 1 6 7 3 0 5]
>>> np.random.seed(0) 
>>> print np.random.permutation(10)
[2 8 4 9 1 6 7 3 0 5]
>>> print np.random.permutation(10) 
[3 5 1 2 9 8 0 6 7 4]
>>> print np.random.permutation(10) 
[2 3 8 4 5 1 0 6 9 7]
>>> print np.random.rand(4) 
[0.64817187 0.36824154 0.95715516 0.14035078]
>>> print np.random.rand(4) 
[0.87008726 0.47360805 0.80091075 0.52047748]