使numpy随机绘制具有一致的可重复性 [英] Making numpy random draws consistent for reproducability

查看:99
本文介绍了使numpy随机绘制具有一致的可重复性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够固定由numpy生成的(伪)随机数的序列,以提高研究的可重复性.通常,我们可以通过修复随机种子来实现此结果,而在Python中,我可以使用np.random.seed来修复该值.但是,在两个Python会话之间,对于同一随机种子,我看到了不同的结果.请在下面的两个会话的输出中找到.

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

推荐答案

np.random.seed 是一个函数.替换:

np.random.seed is a function. Replace:

np.random.seed = 198908

使用:

np.random.seed(198908)

详细信息

seed提供的参数可以是(1)任何整数或(2)任何长度的整数数组(或其他序列),或(3)无.如果它是None,则numpy将从最佳可用随机源中选择一个种子,在Linux上将是/dev/urandom.

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.

这篇关于使numpy随机绘制具有一致的可重复性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆