[英]Numpy array with different standard deviation per row
I'd like to get an NxM
matrix where numbers in each row are random samples generated from different normal distributions(same mean
but different standard deviations). 我想得到一个
NxM
矩阵,其中每行中的数字是从不同的正态分布(相同的mean
但不同的标准偏差)生成的随机样本。 The following code works: 以下代码有效:
import numpy as np
mean = 0.0 # same mean
stds = [1.0, 2.0, 3.0] # different stds
matrix = np.random.random((3,10))
for i,std in enumerate(stds):
matrix[i] = np.random.normal(mean, std, matrix.shape[1])
However, this code is not quite efficient as there is a for
loop involved. 但是,由于涉及
for
循环,此代码效率不高。 Is there a faster way to do this? 有更快的方法吗?
np.random.normal()
is vectorized ; np.random.normal()
是矢量化的 ; you can switch axes and transpose the result: 你可以切换轴并转置结果:
np.random.seed(444)
arr = np.random.normal(loc=0., scale=[1., 2., 3.], size=(1000, 3)).T
print(arr.mean(axis=1))
# [-0.06678394 -0.12606733 -0.04992722]
print(arr.std(axis=1))
# [0.99080274 2.03563299 3.01426507]
That is, the scale
parameter is the column-wise standard deviation, hence the need to transpose via .T
since you want row-wise inputs. 也就是说,
scale
参数是逐列标准偏差,因此需要通过.T
进行转置,因为你需要逐行输入。
How about this? 这个怎么样?
rows = 10000
stds = [1, 5, 10]
data = np.random.normal(size=(rows, len(stds)))
scaled = data * stds
print(np.std(scaled, axis=0))
Output: 输出:
[ 0.99417905 5.00908719 10.02930637]
This exploits the fact that a two normal distributions can be interconverted by linear scaling (in this case, multiplying by standard deviation). 这利用了这样的事实:两个正态分布可以通过线性缩放相互转换(在这种情况下,乘以标准偏差)。 In the output, each column (second axis) will contain a normally distributed variable corresponding to a value in
stds
. 在输出中,每列(第二轴)将包含与
stds
的值对应的正态分布变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.