Ryan
Ryan

Reputation: 3709

Generate two-dimensional normal distribution given a mean and standard deviation

I'm looking for a two-dimensional analog to the numpy.random.normal routine, i.e. numpy.random.normal generates a one-dimensional array with a mean, standard deviation and sample number as input, and what I'm looking for is a way to generate points in two-dimensional space with those same input parameters.

Looks like numpy.random.multivariate_normal can do this, but I don't quite understand what the cov parameter is supposed to be. The following excerpt describes this parameter in more detail and is from the scipy docs:

Covariance matrix of the distribution. Must be symmetric and positive-semidefinite for “physically meaningful” results.

Later in the page, in the examples section, a sample cov value is given:

cov = [[1,0],[0,100]] # diagonal covariance, points lie on x or y-axis

The concept is still quite opaque to me, however.

If someone could clarify what the cov should be or suggest another way to generate points in two-dimensional space given a mean and standard deviation using python I would appreciate it.

Upvotes: 4

Views: 11612

Answers (3)

samet gok
samet gok

Reputation: 110

normal generated random points,

np.random.normal(10, 5, size=[100, 2])

10 is the mean value, 5 is the standart deviation.

Upvotes: 2

Rajesh
Rajesh

Reputation: 1

For random samples from N(mu, sigma^2), use:

sigma * np.random.randn(...) + mu

Examples

np.random.randn() 2.1923875335537315 #random

Two-by-four array of samples from N(3, 6.25):

2.5 * np.random.randn(2, 4) + 3

array( [[-4.49401501, 4.00950034, -1.81814867, 7.29718677],
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]])

https://numpy.org/doc/1.15/reference/generated/numpy.random.randn.html

Upvotes: -3

tamasgal
tamasgal

Reputation: 26259

If you pass size=[1, 2] to the normal() function, you get a 2D-array, which is actually what you're looking for:

>>> numpy.random.normal(size=[1, 2])
array([[-1.4734477 , -1.50257962]])

Upvotes: 6

Related Questions