What does numpy.random.seed (0) do?

What does np.random.seed do in the code below from the Scikit-Learn tutorial? I am not very familiar with the NumPy random state generator, so I am very grateful for the explanation of this layman.

np.random.seed(0) indices = np.random.permutation(len(iris_X)) 
+59
numpy
Feb 01 '14 at 5:28
source share
4 answers

np.random.seed(0) makes unpredictable random numbers

 >>> 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]) 

When sowing reset (each time), the same set of numbers will be displayed each time.

If the random seed is not reset, different numbers appear with each call:

 >>> 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]) 

(pseudo) random numbers work, starting with a number (seed), multiplying it by a large number, then taking modulo this product. The resulting number is then used as a seed to generate the next "random" number. When you plant a seed (each time), it does the same every time, giving you the same numbers.

If you want seemingly random numbers, do not set the seed. However, if you have code that uses random numbers that you want to debug, it can be very useful to set the seed before each run so that the code does the same every time it runs.

To get the most random numbers for each run, call numpy.random.seed() . This will cause numpy to set the seed to a random number obtained from /dev/urandom or its Windows counterpart, or, if none of them are available, will use the clock.

+136
Feb 01 '14 at 5:48
source share

As noted, numpy.random.seed (0) sets the random seed to 0, so the pseudorandom numbers you get from the random start at the same point. This can be useful for debugging in some cases. HOWEVER, after some reading, this seems like the wrong way to go for it if you have threads, because it is not thread safe.

from 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 is unsafe to use if you have many different threads of execution, since it is not guaranteed to work if two different threads perform the function at the same time. If you are not using threads, and if you can reasonably expect that you will not need to rewrite your program this way in the future, numpy.random.seed () should be fine for testing purposes. If there is any reason to suspect that you will need threads in the future, it is much safer in the long run to do as suggested and 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).

an example of how to do this:

 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) 

can give:

[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]

Finally, note that there may be cases where initialization to 0 (as opposed to a seed that does not have all bits 0) can lead to uneven distributions for the first few iterations due to how xor works, but it depends on the algorithm and goes beyond my current concerns and the scope of this issue.

+5
Aug 19 '14 at 9:07
source share

If you set np.random.seed(a_fixed_number) every time you call numpy another random function, the result will be the same:

enter image description here

However, if you just call it once and use various random functions, the results will still be different:

enter image description here

+5
Oct. 25 '16 at 23:48
source share

Using np.Random.Seed (i), where "i" can be any integer, you will ensure that when generating random numbers, you will generate the same set of numbers in a different sequence each time until the next seed is provided

-one
Jun 16 '17 at 20:42 on
source share



All Articles