是否可以使用NumPy复制MATLAB的randn()? [英] Is it possible to reproduce randn() of MATLAB with NumPy?

查看:65
本文介绍了是否可以使用NumPy复制MATLAB的randn()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否可以用NumPy精确再现MATLAB的randn()的整个序列.我用Python/Numpy编写了自己的例程,它给我的结果与其他人的MATLAB代码略有不同,而且由于随机抽奖的不同,我很难找出它的来源.

I wonder if it is possible to exactly reproduce the whole sequence of randn() of MATLAB with NumPy. I coded my own routine with Python/Numpy, and it is giving me a little bit different results from the MATLAB code somebody else did, and I am having hard time finding out where it is coming from because of different random draws.

我发现numpy random.seed值在第一次绘制时产生相同的数字,但是从第二次绘制开始,它是完全不同的.我正在绘制20,000次多元法线,所以我不想只保存matlab绘制并在Python中读取它.如果还有其他方法,我想我必须这样做.请让我知道.

I have found the numpy random.seed value which produces the same number for the first draw, but from the second draw and on, it is completely different. I'm drawing multivariate normal for like 20,000 times so I don't want to just save the matlab draws and read it in Python. If there is any other way I guess I have to do that. Please let me know.

-Joon

推荐答案

用户询问是否可以重现Matlab而非rand的randn()的输出.我无法设置算法或种子来重现randn()的确切数字,但是下面的解决方案对我有用.

The user asked if it was possible to reproduce the output of randn() of Matlab, not rand. I have not been able to set the algorithm or seed to reproduce the exact number for randn(), but the solution below works for me.

在Matlab中:生成正态分布的随机数,如下所示:

In Matlab: Generate your normal distributed random numbers as follows:

rng(1);
norminv(rand(1,5),0,1)
ans = 
   -0.2095    0.5838   -3.6849   -0.5177   -1.0504

在Python中:如下生成正态分布的随机数:

In Python: Generate your normal distributed random numbers as follows:

import numpy as np
from scipy.stats import norm
np.random.seed(1)
norm.ppf(np.random.rand(1,5))
array([[-0.2095,  0.5838, -3.6849, -0.5177,-1.0504]])

从Matlab移至Python或反之亦然时,具有可以重现相等随机数的函数非常方便.

It is quite convenient to have functions, which can reproduce equal random numbers, when moving from Matlab to Python or vice versa.

这篇关于是否可以使用NumPy复制MATLAB的randn()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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