Making numpy random draws consistent for reproduca

2019-02-28 07:10发布

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

1条回答
闹够了就滚
2楼-- · 2019-02-28 07:11

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.

查看更多
登录 后发表回答