How to generate data from a regular distribution

I need to generate a dataset from the normal distribution of $ N = (\ mu, \ sigma ^ 2) $. How to generate this data using Python. $ \ Mu $ and $ \ sigma $ set

+4
source share
2 answers

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 more documentation here .

+2
source

You can calculate 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 this function

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

print y_vals2
+1
source

All Articles