gpt4 book ai didi

Python:从 NxM 正态分布中抽取的 NxM 样本数组

转载 作者:太空宇宙 更新时间:2023-11-03 14:12:41 25 4
gpt4 key购买 nike

我有两个二维数组(或更高维度),一个定义平均值 (M),一个定义标准差 (S)。是否有一个 python 库(numpy、scipy、...?)允许我生成一个数组 (X),其中包含从相应分布中抽取的样本?

换句话说:每个条目 xij 都是来自正态分布的样本,正态分布由相应的均值 mij 和标准差 sij.

最佳答案

是的 numpy 可以在这里提供帮助:

有一个np.random.normal接受类数组输入的函数:

import numpy as np
means = np.arange(10) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
stddevs = np.ones(10) # [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

samples = np.random.normal(means, stddevs)
array([-1.69515214, -0.20680708,  0.61345775,  2.98154162,  2.77888087,        7.22203785,  5.29995343,  8.52766436,  9.70005434,  9.58381479])

even if they are multidimensional:

means = np.arange(10).reshape(2,5) # make it multidimensional with shape 2, 5
stddevs = np.ones(10).reshape(2,5)

samples = np.random.normal(means, stddevs)
array([[-0.76585438,  1.22226145,  2.85554809,  2.64009423,  4.67255324],       [ 3.21658151,  4.59969355,  6.87946817,  9.14658687,  8.68465692]])

The second one has a shape of (2,5)


In case you want only different means but the same standard deviation you can also only pass one array and one scalar and still get an array with the right shape:

means = np.arange(10)

samples = np.random.normal(means, 1)
array([ 0.54018686, -0.35737881,  2.08881115,  3.08742942,  4.4426366 ,        3.6694955 ,  5.27515536,  8.68300816,  8.83893819,  7.71284217])

关于Python:从 NxM 正态分布中抽取的 NxM 样本数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36097454/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com