简体   繁体   English

Numpy 每列均值和标准差不同的数组

[英]Numpy array with different mean and standard deviation per column

i would like to get an numpy array, shape 1000 row and 2 column.我想得到一个 numpy 数组,形状为 1000 行和 2 列。

  1. 1st column will contain - Gaussian distributed variables with standard deviation 2 and mean 1.第一列将包含 - 标准差为 2 且均值为 1 的高斯分布变量。
  2. 2nd column will contain Gaussian distributed variables with mean -1 and standard deviation 0.5.第二列将包含均值为 -1 且标准差为 0.5 的高斯分布变量。

How to create the array using define value of mean and std?如何使用均值和标准差的定义值创建数组?

You can use numpy's random generators.您可以使用 numpy 的随机生成器。

import numpy as np

# as per kwinkunks suggestion
rng = np.random.default_rng()

arr1 = rng.normal(1, 2, 1000).reshape(1000, 1)
arr2 = rng.normal(-1, 0.5, 1000).reshape(1000, 1)

arr1[:5]

array([[-2.8428678 ],
       [ 2.52213097],
       [-0.98329961],
       [-0.87854616],
       [ 0.65674208]])

arr2[:5]

array([[-0.85321735],
       [-1.59748405],
       [-1.77794019],
       [-1.02239036],
       [-0.57849622]])

After that, you can concatenate.之后,您可以连接。

np.concatenate([arr1, arr2], axis = 1)

# output
array([[-2.8428678 , -0.85321735],
       [ 2.52213097, -1.59748405],
       [-0.98329961, -1.77794019],
       ...,
       [ 0.84249042, -0.26451526],
       [ 0.6950764 , -0.86348222],
       [ 3.53885426, -0.95546126]])

Use np.random.normal directly:直接使用np.random.normal

import numpy as np
np.random.normal([1, -1], [2, 0.5], (1000, 2))

You can just create two normal distributions with the mean and std for each and stack them.您可以只创建两个normal ,每个均值和标准差,然后堆叠它们。

np.hstack((np.random.normal(1, 2, size=(1000,1)), np.random.normal(-1, 0.5, size=(1000,1))))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM