Consistency in Python results

Asked

Viewed 87 times

2

Hello, I’m studying python code and I came across this line:

np.random.seed(0)

where it says consistency of results. I searched, but I still can’t understand the function of this command. Could anyone explain or indicate where I can find documentation?

Thanks a lot of help.

1 answer

3


np.random.seed(0) defines that the random generation of numbers will follow a predefined pattern.

In doing np.random.seed(0), the next call from numpy.random.rand(n), being n the size of the vector to be generated will follow a fixed pattern.

For example, without setting a Seed, equal calls from numpy.random.rand(n) would return different results.

>>> numpy.random.rand(5)
array([0.79172504, 0.52889492, 0.56804456, 0.92559664, 0.07103606])
>>> numpy.random.rand(5)
array([0.64589411, 0.43758721, 0.891773  , 0.96366276, 0.38344152])

But when setting the Seed before calling numpy.random.rand(n), you ensure that the generated sequence will be equal / will follow the same pattern every time.

>>> numpy.random.seed(0)
>>> numpy.random.rand(5)
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])
>>> numpy.random.seed(0)
>>> numpy.random.rand(5)
array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])

Note that when calling numpy.random.rand(5), a Seed is reset, it is necessary to arrow it novamento with numpy.random.seed(0).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.