Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Numpy random seed valid for entire jupyter notebook

I'm using functions from numpy.random on a Jupyter Lab notebook and I'm trying to set the seed using numpy.random.seed(333). This works as expected only when the seed setting is in the same notebook cell as the code. For example, if I have a script like this:

import numpy as np

np.random.seed(44)

ll = [3.2,77,4535,123,4]

print(np.random.choice(ll))
print(np.random.choice(ll))

The output from both np.random.choice(ll) will be same, because the seed is set:

# python seed.py
4.0 
123.0
# python seed.py
4.0
123.0

Now, if I try to do the same on the Jupyter notebook, I get different results:

# in [11]
import numpy as np
# even if I set the seed here the other cells don't see it
np.random.seed(333)

# in [12]
np.random.choice([1,23,44,3,2])
23
# gets the same numbers

# in [13]
np.random.choice([1,23,44,3,2])
44
# gets different numbers every time I run this cell again

Is there a way to set the numpy random seed globally in a Jupyter lab notebook?

like image 1000
Ivan Avatar asked Jul 19 '18 14:07

Ivan


People also ask

Is NumPy random seed Global?

The Problem With NumPy's Global Random Seed Using np. random. seed(number) sets what NumPy calls the global random seed, which affects all uses to the np. random.

Does random seed work for NumPy?

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.

What is the default NumPy random seed?

As noted, numpy. random. seed(0) sets the random seed to 0, so the pseudo random numbers you get from random will start from the same point.

Does Jupyter support code autocompletion?

Yes you have auto-complete built-in Jupyter, like you have in any other Jupyter environment. Simply hit the "Tab" key while writing code.

What is random seed 0 in NumPy?

NumPy.random.seed (0) sets the random seed to ‘0’. The pseudo-random numbers generated with seed value 0 will start from the same point every time. NumPy.random.seed (0) is widely used for debugging in some cases. Output on two executions:

How does NumPy random randint work?

NumPy random seed sets the seed for the pseudo-random number generator, and then NumPy random randint selects 5 numbers between 0 and 99. Let’s just run the code so you can see that it reproduces the same output if you have the same seed.

What is pseudo-random number generator in NumPy?

These algorithms are called “pseudo-random number generators. ” NumPy random seed functions generate random numbers based on “pseudo-random number generators ” algorithms. NumPy random () function generates pseudo-random numbers based on some value.

How to set the seed of a random number generator in Python?

However, a lot of analysis relies on random numbers being used. In Python, you can set the seed for the random number generator to achieve repeatable results with the random_seed () function.


2 Answers

Because you're repeatedly calling randint, it generates different numbers each time. It's important to note that seed does not make the function consistently return the same number, but rather makes it such that the same sequence of numbers will be produced if you repeatedly run randint the same amount of times. Therefore, you're getting the same sequence of numbers every time you re-run the random.randint, rather than it always producing the same number.

Re-seeding in that specific cell, before each random.randint call, should work, if you want the same random number every single time. Otherwise, you can expect to consistently get the same sequence of numbers, but not to get the same number every time.

like image 61
Sam Carpenter Avatar answered Sep 19 '22 12:09

Sam Carpenter


Because you run np.random.choice() in the cells different from np.random.seed(). Try to run np.random.seed() and np.random.choice() in the same cell, you'll get same number.

# in [11]
import numpy as np
# even if I set the seed here the other cells don't see it
np.random.seed(333)
np.random.choice([1,23,44,3,2])
2
# gets the same numbers

# in [12]
import numpy as np
# even if I set the seed here the other cells don't see it
np.random.seed(333)
np.random.choice([1,23,44,3,2])
2
# gets the same numbers
like image 26
allenyllee Avatar answered Sep 20 '22 12:09

allenyllee