如何使用 Numpy 生成不相关的样本
我想在Python上生成随机样品,但每个样品都有自己的标准偏差。
我以为我可以使用 np.random.normal(0,scale = np.Array(standard_deviation),size =(len(np.Array(standard_deviation))),number_of_simulations)
,
但是,当我放置一个时,颠簸似乎不起作用 (这与我从文档中理解的相反),只希望浮动作为参数。
刻度的数组 np.random.normal(0,scale = np.Array(standard_deviation)[i],size =(1,number_of_simulations)
我想我可以做一个循环,然后加成每个结果,但我不''如果您不需要,请这样做,因为您相信我会清楚地循环
。
I'd like to generate random samples on python, but each with their own standard deviation.
I thought I could usenp.random.normal(0, scale=np.array(standard_deviation), size=(len(np.array(standard_deviation)), number_of_simulations)
However, bumpy seems not to work when I put an array for scale (which is contrary to what I understand from the documentation) and only wants a float as an argument.
My goal is to render an array of size (Number of standards deviations X Numbers of simulations) where each row is justnp.random.normal(0, scale=np.array(standard_deviation)[i], size= (1,number_of_simulations)
I think I could do a loop and then concatenate each result but i don't want to do this if not necessary because you loose the interest of Numpy and Pandas by doing loops I believe.
I hope I was clear and thanks for your help !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
numpy随机函数 do 接受数组,但是当您提供
size
参数时,形状必须兼容。更改以下内容:
结果
将具有Shape
(number_of_simulations,len(standard_deviation))
。这是Ipython会话中的一个具体示例。请注意,我不使用
numpy.random.normal
,而是使用较新的numpy Random API,其中我创建了一个称为rng
的生成器,并调用其normal()
方法:The NumPy random functions do accept arrays, but when you also give a
size
parameter, the shapes must be compatible.Change this:
to
The result will have shape
(number_of_simulations, len(standard_deviation))
.Here's a concrete example in an ipython session. Note that instead of using
numpy.random.normal
, I use the newer NumPy random API, in which I create a generator calledrng
and call itsnormal()
method: