6

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.

1
  • It also works for the scale parameter, like np.random.normal(loc=np.zeros((4,4)),scale=np.array(range(1,17)).reshape((4,4))) Commented May 23, 2016 at 20:37

2 Answers 2

8

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)
Out[10]: 
array([  9.45865328e-01,   1.11542264e+01,   9.88601302e+01,
         1.00120448e+03])

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 ])
Sign up to request clarification or add additional context in comments.

Comments

1

As a supplement to the above comments and example, the documentation is there in my implementation of numpy.
In abbreviated form, with large snips:

help(np.random.normal)
    normal(loc=0.0, scale=1.0, size=None)    
    Draw random samples from a normal (Gaussian) distribution.
    Parameters
    ----------
    loc : float    Mean ("centre") of the distribution.
    scale : float  Standard deviation (spread or "width") of the  
                   distribution.
    size : tuple of ints,   Output shape.
           If the given shape is, e.g., ``(m, n, k)``, then
           ``m * n * k`` samples are drawn.

If you wish to produce a number of data values with a particular shape, centered about a mean with a specified standard deviation, you could do the following.

>>> x = 10
>>> vals = np.random.normal(x,3.,(10,))
>>> vals
array([ 10.6999745 ,   9.58139692,  14.04490407,   9.54797132,
        10.18378835,  11.42772729,   5.22100578,   9.51757533,
        12.95314676,  13.77068901])

which generates an array of 10 values, shape (10,) with a mean of 10 and values spread within +/- 3 std deviations. The actual distribution function, references and a matplotlib code sample are also provided. I am using np.version.version '1.8.0'.

This is a useful function should you want to produce point samples (X,Y) centered about a mean value with a known spread.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.