在Numpy中包含的随机数生成函数如下表所示:
函数 | 函数功能 |
---|---|
rand(d0, d1, …, dn) | 返回输入数组维度对应的矩阵 |
randn(d0, d1, …, dn) | 返回输入数组维度对应服从标准正太分布的矩阵 |
randint(low[, high, size, dtype]) | 返回范围为[low, high)范围的整型随机数据 |
random_integers(low[, high, size]) | 返回[low, high]之间的随机整数 |
random_sample([size]) | 返回[0.0, 1.0)之间的浮点随机数 |
random([size]) | 从范围[0.0, 1.0)返回浮点随机数 |
ranf([size]) | 从范围[0.0, 1.0)返回浮点随机数 |
sample([size]) | 从范围[0.0, 1.0)返回浮点随机数 |
choice(a[, size, replace, p]) | 从给出的1维数组中产生其随机采样 |
bytes(length) | 返回随机字节 |
该函数返回规定维度的随机矩阵,随机数是来源于服从[1,0)分布的采样。
示例:
np.random.rand(3, 4)
[[ 0.9453398 0.15785589 0.14297825 0.40554182]
[ 0.58353036 0.16330881 0.79096958 0.29872379]
[ 0.30474484 0.85217927 0.06831362 0.61730196]]
该函数返回规定维度的随机矩阵,随机数是来源于服从(0,1)标准正太分布的采样。该函数与random.standard_normal类似,可以通过如下算式得到相应的正太分布N(mu, sigma^2):
sigma * np.random.randn(…) + mu
示例:
np.random.randn(3, 4)
[[-2.30951289 -1.05847819 -0.06452076 -0.82147271]
[ 0.324241 -0.51254897 0.51067497 0.66082303]
[-0.0982416 0.78864197 -0.80479118 2.2884627 ]]
该函数返回服从[low, high)离散均匀分布的随机数矩阵。若果high参数没有指定则范围将被限定为[0, low)
示例:
np.random.randint(0, 3, (3, 4))
[[1 0 2 0]
[0 2 0 2]
[2 2 2 1]]
该函数与上一个函数类似,区别是返回服从[low, high]离散均匀分布的随机数矩阵。若果high参数没有指定则范围将被限定为[1, low]
示例:
np.random.random_integers(0, 3, (3, 4))
[[2 2 0 3]
[2 0 1 2]
[3 3 2 0]]
产生a与b之间的N个均匀整数:
a + (b - a) * (np.random.random_integers(N) - 1) / (N - 1.)
该函数返回范围为 [0.0, 1.0)的连续均值浮点数分布,若是需要产生的数范围为[a,b),则:
(b - a) * random_sample() + a
示例:
np.random.random_sample((3, 4))
[[ 0.12439296 0.44063728 0.65585181 0.29929493]
[ 0.93312505 0.61461946 0.15346194 0.11332448]
[ 0.35118524 0.31794849 0.69337822 0.73912451]]
该函数返回的是一维数组a中的抽取矩阵,若a是一个数字则一维数字便是np.arange(a)。
示例:
a = [2, 4, 6, 8, 10]
np.random.choice(a, (3, 4))
[[ 2 2 6 2]
[ 4 10 2 8]
[ 8 10 6 2]]
返回随机字节
示例:
np.random.bytes(5)
l�;�
函数 | 具体分布 |
---|---|
beta(a, b[, size]) | Draw samples from a Beta distribution. |
binomial(n, p[, size]) | Draw samples from a binomial distribution. |
chisquare(df[, size]) | Draw samples from a chi-square distribution. |
dirichlet(alpha[, size]) | Draw samples from the Dirichlet distribution. |
exponential([scale, size]) | Draw samples from an exponential distribution. |
f(dfnum, dfden[, size]) | Draw samples from an F distribution. |
gamma(shape[, scale, size]) | Draw samples from a Gamma distribution. |
geometric(p[, size]) | Draw samples from the geometric distribution. |
gumbel([loc, scale, size]) | Draw samples from a Gumbel distribution. |
hypergeometric(ngood, nbad, nsample[, size]) | Draw samples from a Hypergeometric distribution. |
laplace([loc, scale, size]) | Draw samples from the Laplace or double exponential distribution with specified location (or mean) and scale (decay). |
logistic([loc, scale, size]) | Draw samples from a logistic distribution. |
lognormal([mean, sigma, size]) | Draw samples from a log-normal distribution. |
logseries(p[, size]) | Draw samples from a logarithmic series distribution. |
multinomial(n, pvals[, size]) | Draw samples from a multinomial distribution. |
multivariate_normal(mean, cov[, size, …) | Draw random samples from a multivariate normal distribution. |
negative_binomial(n, p[, size]) | Draw samples from a negative binomial distribution. |
noncentral_chisquare(df, nonc[, size]) | Draw samples from a noncentral chi-square distribution. |
noncentral_f(dfnum, dfden, nonc[, size]) | Draw samples from the noncentral F distribution. |
normal([loc, scale, size]) | Draw random samples from a normal (Gaussian) distribution. |
pareto(a[, size]) | Draw samples from a Pareto II or Lomax distribution with specified shape. |
poisson([lam, size]) | Draw samples from a Poisson distribution. |
power(a[, size]) | Draws samples in [0, 1] from a power distribution with positive exponent a - 1. |
rayleigh([scale, size]) | Draw samples from a Rayleigh distribution. |
standard_cauchy([size]) | Draw samples from a standard Cauchy distribution with mode = 0. |
standard_exponential([size]) | Draw samples from the standard exponential distribution. |
standard_gamma(shape[, size]) | Draw samples from a standard Gamma distribution. |
standard_normal([size]) | Draw samples from a standard Normal distribution(mean=0, stdev=1). |
standard_t(df[, size]) | Draw samples from a standard Student’s t distribution with df degrees of freedom. |
triangular(left, mode, right[, size]) | Draw samples from the triangular distribution over the interval [left, right]. |
uniform([low, high, size]) | Draw samples from a uniform distribution. |
vonmises(mu, kappa[, size]) | Draw samples from a von Mises distribution. |
wald(mean, scale[, size]) | Draw samples from a Wald, or inverse Gaussian, distribution. |
weibull(a[, size]) | Draw samples from a Weibull distribution. |
zipf(a[, size]) | Draw samples from a Zipf distribution. |
这里就是用最常用的高斯分布作为示例进行讲解,其它分的使用也是类似的。
mu = 50
sigma = 10.0
a = np.linspace(0, 100, 1000)
y = 1/(sigma * np.sqrt(2 * np.pi))*np.exp(-(a - mu)**2 / (2 * sigma**2))
data = np.random.normal(mu, sigma, 1000)
plt.figure()
plt.hist(data, 50, normed=True)
plt.plot(a, y, 'r-')
plt.show()