Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

numpy

What does np.random.seed do in the below code from a Scikit-Learn tutorial? I'm not very familiar with NumPy's random state generator stuff, so I'd really appreciate a layman's terms explanation of this.

np.random.seed(0) indices = np.random.permutation(len(iris_X)) 
like image 506
covariance Avatar asked Feb 01 '14 05:02

covariance


People also ask

What does random seed 0 do in Python?

Python Random seed() Method The seed() method is used to initialize the random number generator. The random number generator needs a number to start with (a seed value), to be able to generate a random number. By default the random number generator uses the current system time.

What does numpy random seed do?

The numpy random seed is a numerical value that generates a new set or repeats pseudo-random numbers. The value in the numpy random seed saves the state of randomness. If we call the seed function using value 1 multiple times, the computer displays the same random numbers.

Why do we use random seed 42?

However, Douglas Adams himself revealed the reason why he chose 42 in this message. “It was a joke. It had to be a number, an ordinary, smallish number, and I chose that one. I sat at my desk, stared into the garden and thought '42 will do!

How do you generate a random number between 0 and 1 in numpy?

random. random() Return the next random floating point number in the range [0.0, 1.0). But if your inclusion of the numpy tag is intentional, you can generate many random floats in that range with one call using a np. random function.


1 Answers

np.random.seed(0) makes the random numbers predictable

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

With the seed reset (every time), the same set of numbers will appear every time.

If the random seed is not reset, different numbers appear with every invocation:

>>> 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 by starting with a number (the seed), multiplying it by a large number, adding an offset, then taking modulo of that sum. The resulting number is then used as the seed to generate the next "random" number. When you set the seed (every time), it does the same thing every time, giving you the same numbers.

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

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 analog or, if neither of those is available, it will use the clock.

For more information on using seeds to generate pseudo-random numbers, see wikipedia.

like image 98
John1024 Avatar answered Oct 12 '22 22:10

John1024