2016-03-28 145 views
4

我需要从正态分布$ N =(\ mu,\ sigma^2)$生成数据集。如何使用Python生成这些数据。的$ \亩$和$ \西格玛$值给出如何从正态分布生成数据

+0

通过数据? –

+0

是从正态分布产生数据 –

回答

2

使用numpy.random.normal

如果你想从标准正态分布产生1000个样品,你可以简单地做

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

你可以阅读文档here获取更多详细信息。

+0

Many thanx @Banach Tarski。我会尝试。 –

1

您可以手动计算它

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 

还是你的意思产生*样本*从正态分布您可以使用特定的函数

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

print y_vals2 
+0

谢谢@ jrhee让我试试它。 –

相关问题