Home > Software engineering >  Numpy array with different mean and standard deviation per column
Numpy array with different mean and standard deviation per column

Time:12-02

i would like to get an numpy array , shape 1000 row and 2 column.

  1. 1st column will contain - Gaussian distributed variables with standard deviation 2 and mean 1.
  2. 2nd column will contain Gaussian distributed variables with mean -1 and standard deviation 0.5.

How to create the array using define value of mean and std?

CodePudding user response:

You can just create two normal distributions with the mean and std for each and stack them.

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

CodePudding user response:

You can use numpy's random generators.

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]])

CodePudding user response:

Use np.random.normal directly:

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