Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
557 views
in Technique[技术] by (71.8m points)

python - Making numpy random draws consistent for reproducability

I would like to be able to fix the sequence of (pseudo) random numbers being generated by numpy for reproducability of my research. We can usually achieve this result by fixing the random seed and I followed the same in Python by using np.random.seed to fix the value. However, between two Python sessions I am seeing different results for the same random seed. Please find below the output from two sessions.

Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.random.seed = 198908
>>> np.random.uniform(low=0.0, high=1.0, size=10)
array([ 0.43203804,  0.89881118,  0.02976592,  0.93286634,  0.21568609,
        0.44705267,  0.27159611,  0.4000281 ,  0.33873711,  0.54835523])
>>> exit()

Python 2.7.6 (default, Nov 10 2013, 19:24:24) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> np.random.seed = 198908
>>> np.random.uniform(low=0.0, high=1.0, size=10)
array([ 0.20178185,  0.22492203,  0.51029445,  0.4776532 ,  0.49109006,
        0.22807983,  0.86419725,  0.53422946,  0.08904607,  0.83125896])
>>> exit()

Am I missing something here? Also, can anyone suggest an approach that will generate consistent random draws across different machines (assuming same software version is installed)?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

np.random.seed is a function. Replace:

np.random.seed = 198908

With:

np.random.seed(198908)

Details

The argument provided to seed can be (1) any integer or (2) an array (or other sequence) of integers of any length, or (3) None. If it is None, then numpy will select a seed from the best available random source which on Linux would be /dev/urandom.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...