将numpy.random.normal与数组一起使用

Pea*_*cer 6 python arrays numpy python-3.x

假设我有以下两个带有均值和标准偏差的数组:

mu = np.array([2000, 3000, 5000, 1000])
sigma = np.array([250, 152, 397, 180])
Run Code Online (Sandbox Code Playgroud)

然后:

a = np.random.normal(mu, sigma)

In [1]: a
Out[1]: array([1715.6903716 , 3028.54168667, 4731.34048645, 933.18903575])
Run Code Online (Sandbox Code Playgroud)

但是,如果我要求亩的每个元素100抽奖,西格玛:

a = np.random.normal(mu, sigma, 100)

a = np.random.normal(mu, sigma, 100)
Traceback (most recent call last):

File "<ipython-input-417-4aadd7d15875>", line 1, in <module>
a = np.random.normal(mu, sigma, 100)

File "mtrand.pyx", line 1652, in mtrand.RandomState.normal

File "mtrand.pyx", line 265, in mtrand.cont2_array

ValueError: shape mismatch: objects cannot be broadcast to a single shape
Run Code Online (Sandbox Code Playgroud)

我也尝试使用大小的元组:

s = (100, 100, 100, 100)
a = np.random.normal(mu, sigma, s)
Run Code Online (Sandbox Code Playgroud)

我错过了什么?

cs9*_*s95 3

我不相信当您传递平均值和标准差值的列表/向量时可以控制大小参数。相反,您可以迭代每一对,然后连接:

np.concatenate(
   [np.random.normal(m, s, 100) for m, s in zip(mu, sigma)]
) 
Run Code Online (Sandbox Code Playgroud)

这给你一个(400, )数组。如果您想要一个(4, 100)数组,请调用np.array而不是np.concatenate.