Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Add guassian noise to a numpy array

I have an array a with size n*2, I want to add a guassian noise to each value a[i,j] with mean= a[i,j] and standard deviation = 0.2* a[i,j] to each number in the array. Could you please help me with that?

Here is a sample of array:

import numpy as np
a = np.random.rand(10,2)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you look at the documentation, the np.random.normal function takes an array-like for both the mean and stdev arguments, so

x = np.random.rand(10, 2)
noise = np.random.normal(x, 0.2*x)

x + noise

However, I don’t quite follow your reasoning behind using each value in the array as the mean… if you want to add noise to data, why not use loc=0? That would then add +/- a tiny bit of Gaussian distributed noise to each of the values without heavily skewing each value.

For instance, if x[i,j] == 6, and you added noise centered on ~G(6, 1.2), then x[i, j] would be as large as 12 on average, which isn’t so much adding noise as it is fundamentally changing the data.

Here, taking 6 to be the value at some given coordinate x[i, j],

In [12]: 6 + np.random.normal(6, 0.2*6)
Out[12]: 13.812965429428406

Your 6 turned into 13.81 — is this your intention?

Depending on each of the distributions, you’d could be consistently shifting all your data in the positive direction, which is almost certainly not what you intend. If you’re simply trying to add jitter to your data, you should use loc=0.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading