如何使用 Numpy 生成不相关的样本

发布于 2025-01-21 01:02:32 字数 429 浏览 1 评论 0原文

我想在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 use
np.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 just
np.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沉睡月亮 2025-01-28 01:02:32

numpy随机函数 do 接受数组,但是当您提供size参数时,形状必须兼容。

更改以下内容:

np.random.normal(0, scale=np.array(standard_deviation), size=(len(np.array(standard_deviation)), number_of_simulations)

结果

np.random.normal(0, scale=standard_deviation, size=(number_of_simulations, len(standard_deviation)))

将具有Shape (number_of_simulations,len(standard_deviation))

这是Ipython会话中的一个具体示例。请注意,我不使用numpy.random.normal,而是使用较新的numpy Random API,其中我创建了一个称为rng的生​​成器,并调用其normal()方法:

In [103]: rng = np.random.default_rng()

In [104]: standard_deviation = np.array([1, 5, 25])

In [105]: number_of_simulations = 6

In [106]: rng.normal(scale=standard_deviation, size=(number_of_simulations, len(standard_deviation)))
Out[106]: 
array([[ -0.31088926,   1.95005394,  -8.77983357],
       [  1.80907248,   4.27082827,  31.13457498],
       [ -0.27178958, -12.6589072 , -31.70729135],
       [  0.2848883 ,   1.71198071, -23.6336055 ],
       [  0.78457822,   2.78281586,  32.61089728],
       [ -0.7014944 ,   5.47845616,   5.34276638]])

The NumPy random functions do accept arrays, but when you also give a size parameter, the shapes must be compatible.

Change this:

np.random.normal(0, scale=np.array(standard_deviation), size=(len(np.array(standard_deviation)), number_of_simulations)

to

np.random.normal(0, scale=standard_deviation, size=(number_of_simulations, len(standard_deviation)))

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 called rng and call its normal() method:

In [103]: rng = np.random.default_rng()

In [104]: standard_deviation = np.array([1, 5, 25])

In [105]: number_of_simulations = 6

In [106]: rng.normal(scale=standard_deviation, size=(number_of_simulations, len(standard_deviation)))
Out[106]: 
array([[ -0.31088926,   1.95005394,  -8.77983357],
       [  1.80907248,   4.27082827,  31.13457498],
       [ -0.27178958, -12.6589072 , -31.70729135],
       [  0.2848883 ,   1.71198071, -23.6336055 ],
       [  0.78457822,   2.78281586,  32.61089728],
       [ -0.7014944 ,   5.47845616,   5.34276638]])
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文