NARAYAN CHANGDER
NARAYAN CHANGDER

Reputation: 319

How to generate data from normal distribution

I need to generate Data set from Normal distribution N. How to generate this data using Python. The value of mu and sigma is given.

Upvotes: 6

Views: 8958

Answers (2)

jrhee17
jrhee17

Reputation: 1152

You can compute it manually

import numpy as np

mu = 0;
sigma = 1;

# Generates numbers between -0.5, 0.5
x_vals = np.random.rand(10) - 0.5

# Compute normal distribution from x vals
y_vals = np.exp(-pow(mu - x_vals,2)/(2 * pow(sigma, 2))) / (sigma * np.sqrt(2*np.pi))

print y_vals

Or you can use the given function

# You can also use the randn function
y_vals2 = sigma * np.random.randn(10) + mu

print y_vals2

Upvotes: 1

Banach Tarski
Banach Tarski

Reputation: 1829

Use numpy.random.normal

If you want to generate 1000 samples from the standard normal distribution you can simply do

import numpy
mu, sigma = 0, 1
samples = numpy.random.normal(mu, sigma, 1000)

You can read the documentation here for additional details.

Upvotes: 8

Related Questions