vendredi 20 août 2021

How to change values in a numpy array with a given probability and based on a distribution?

I have a numpy array,

a = np.zeros((5,2))

a = array([[0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.],
           [0., 0.]])

Aim: Each value should have a probability of changing, p = 0.05, and the value it changes to is given by a sample from a normal distribution with mean = 1, st.dev = 0.2

So far, I have tried following This:

a[np.random.rand(*a.shape) < 0.05] = rng.normal(loc=1,scale=0.2)

This does change values randomly with p = 0.05 ,but all values are the same, which is not ideal.

So, how does one go about making sure that each sampled value is independent(without using for loop)?




Aucun commentaire:

Enregistrer un commentaire