Does calling random.normal on an array of values add noise?

I saw this pattern in someone’s code:

import numpy as np
# Create array
xx = np.linspace(0.0, 100.0, num=100)
# Add Noise
xx = np.random.normal(xx)

and it seems to add some noise to each value of the array, but I can’t find any documentation for this. What’s happening? What determines the properties (i.e. scaling) of the noise? Is the given value being treated as the mean (i.e. the loc parameter) of each sampling from the normal distribution?

I’d also be very curious to know why this behavior doesn’t seem to be covered in the documentation.

Best answer

I don’t see it documented either, but many numpy functions that take an ndarray will operate on it element-wise. Anyway, you can easily verify that when passing it an array it call numpy.random.normal for each element on the array using that element’s value as the mean and returns an array:

In [9]: xx = numpy.array([1, 10, 100, 1000])

In [10]: numpy.random.normal(xx)
array([  9.45865328e-01,   1.11542264e+01,   9.88601302e+01,

It appears that it is using the default value of 1.0 for the scale. You can override this though:

In [12]: numpy.random.normal(xx, 10)
Out[12]: array([    8.92500743,    -5.66508088,    97.33440273,  1003.37940455])

In [13]: numpy.random.normal(xx, 100)
Out[13]: array([ -75.13092966,  -47.0841671 ,  154.12913986,  816.3126146 ])