Reputation: 3337
How do I create a numpy array with the dimensions, mean, and variance as parameters? I see there is a numpy.random.randn
function which allows the user to specify dimensions, but that function assumes a mean of 0 and variance of 1.
I am okay with the mean 0 part, but I want to be able to specify a variance each time I am creating a new numpy array.
Thanks for your help!
Upvotes: 2
Views: 3865
Reputation: 582
From the numpy documentation (numpy.random.randn
):
For random samples from , when mu is the mean and sigma is the variance (see Normal Distribution), use:
sigma * np.random.randn(d0, d1, ..., dn) + mu
When (d0, d1, ..., dn)
is the shape of the generated array.
Upvotes: 3
Reputation: 164653
You may be looking for numpy.random.normal
. For example:
import numpy as np
arr = np.random.normal(loc=1, scale=0.50, size=(500, 500))
print(arr.mean()) # 0.9995707343806642
print(arr.std()) # 0.5010967322495354
Here loc
represents the mean and scale
the standard deviation, i.e. the square root of variance.
Of course, you are drawing samples from a distribution, so you will not have a mean of 1.0 or a standard deviation of 0.50, unless you have a very large array.
Upvotes: 4