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

随机种子指定计算机生成随机数序列时的起始点。

For example, let’s say you wanted to generate a random number in Excel (Note: Excel sets a limit of 9999 for the seed). If you enter a number into the Random Seed box during the process, you’ll be able to use the same set of random numbers again. If you typed “77” into the box, and typed “77” the next time you run the random number generator, Excel will display that same set of random numbers. If you type “99”, you’ll get an entirely different set of numbers. But if you revert back to a seed of 77, then you’ll get the same set of random numbers you started with.

例如,“取一个数x,加上900 +x,然后减去52。”为了使进程开始,您必须指定一个起始数字x(种子)。让我们以77为例:

900 + 77 = 977 减去52 = 925 按照相同的算法,第二个“随机”数将是:

900 + 925 = 1825 减去52 = 1773 这个简单的例子遵循一个模式,但是计算机数字生成背后的算法要复杂得多

如果你每次调用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]

想象一下,您正在向某人展示如何用一堆“随机”数字编写代码。通过使用numpy种子,它们可以使用相同的种子号并获得相同的“随机”数字集。

所以它不是完全随机的,因为算法会吐出数字但它看起来像是随机生成的一堆。

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

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
.
.
.

我希望这对你有所帮助!