在 Python、NumPy 和 R 中创建相同的随机数序列 [英] Creating same random number sequence in Python, NumPy and R

查看:15
本文介绍了在 Python、NumPy 和 R 中创建相同的随机数序列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python、NumPy 和 R 都使用相同的算法 (Mersenne Twister) 来生成随机数序列.因此,从理论上讲,设置相同的种子应该会在所有 3 个中产生相同的随机数序列.事实并非如此.我认为这 3 个实现使用了导致这种行为的不同参数.

<前>电阻>set.seed(1)>runif(5)[1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819

<前>Python在 [3] 中:random.seed(1)在 [4] 中:[random.random() for x in range(5)]出[4]:[0.13436424411240122,0.8474337369372327,0.763774618976614,0.2550690257394217,0.49543508709194095]

NumPy在 [23] 中:将 numpy 导入为 np在 [24]: np.random.seed(1)在 [25]: np.random.rand(5)出[25]:数组([ 4.17022005e-01, 7.20324493e-01, 1.14374817e-04,3.02332573e-01, 1.46755891e-01])

有什么方法可以让 NumPy 和 Python 实现产生相同的随机数序列?当然,正如一些评论和答案所指出的,可以使用 rpy.我特别想要的是微调 Python 和 NumPy 中各自调用中的参数以获取序列.

上下文:关注来自使用 R 的 EDX 课程.在其中一个论坛上,有人问是否可以使用 Python,工作人员回答说,有些作业需要设置特定的种子并提交答案.

相关:

  1. 比较 Matlab 和 Numpy 代码使用随机数生成由此看来,底层的 NumPy 和 Matlab 实现是相似的.
  2. python vs 八度随机发生器:这个问题确实来得公平接近预期的答案.需要对默认状态生成器进行某种包装.

解决方案

在python中使用rpy2调用r,这里有一个demo,numpy数组data正在分享R 中带有 x 的内存:

import rpy2.robjects 作为robjects数据 = robjects.r("""set.seed(1)x <- runif(5)""")打印 np.array(data)数据[1] = 1.0打印 robjects.r["x"]

Python, NumPy and R all use the same algorithm (Mersenne Twister) for generating random number sequences. Thus, theoretically speaking, setting the same seed should result in same random number sequences in all 3. This is not the case. I think the 3 implementations use different parameters causing this behavior.

R
>set.seed(1)
>runif(5)
[1] 0.2655087 0.3721239 0.5728534 0.9082078 0.2016819

Python
In [3]: random.seed(1)

In [4]: [random.random() for x in range(5)]
Out[4]: 
[0.13436424411240122,
 0.8474337369372327,
 0.763774618976614,
 0.2550690257394217,
 0.49543508709194095]

NumPy
In [23]: import numpy as np

In [24]: np.random.seed(1)
In [25]: np.random.rand(5)
Out[25]: 
array([  4.17022005e-01,   7.20324493e-01,   1.14374817e-04,
         3.02332573e-01,   1.46755891e-01])

Is there some way, where NumPy and Python implementation could produce the same random number sequence? Ofcourse as some comments and answers point out, one could use rpy. What I am specifically looking for is to fine tune the parameters in the respective calls in Python and NumPy to get the sequence.

Context: The concern comes from an EDX course offering in which R is used. In one of the forums, it was asked if Python could be used and the staff replied that some assignments would require setting specific seeds and submitting answers.

Related:

  1. Comparing Matlab and Numpy code that uses random number generation From this it seems that the underlying NumPy and Matlab implementation are similar.
  2. python vs octave random generator: This question does come fairly close to the intended answer. Some sort of wrapper around the default state generator is required.

解决方案

use rpy2 to call r in python, here is a demo, the numpy array data is sharing memory with x in R:

import rpy2.robjects as robjects

data = robjects.r("""
set.seed(1)
x <- runif(5)
""")

print np.array(data)

data[1] = 1.0

print robjects.r["x"]

这篇关于在 Python、NumPy 和 R 中创建相同的随机数序列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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