Numpy random number generator function and common distribution function explained

Numpy random numbers

1. Numpy random number overview

The random number generation functions included in Numpy are shown in the following table:

function function
rand (d0, d1,…, dn) Returns the matrix corresponding to the dimension of the input array
randn(d0, d1, …, dn) Returns the matrix of the input array dimension corresponding to the standard normal distribution
randint(low[, high, size, dtype]) Returns integer random data in the range [low, high)
random_integers(low[, high, size]) Returns a random integer between [low, high]
random_sample([size]) Returns a floating point random number between [0.0, 1.0)
random([size]) Returns a floating point random number from the range [0.0, 1.0)
ranf([size]) Returns a floating point random number from the range [0.0, 1.0)
sample([size]) Returns a floating point random number from the range [0.0, 1.0)
choice(a[, size, replace, p]) Generate random samples from the given 1D array
bytes(length) return random bytes

2. Random number generation example

2.1 rand (d0, d1,…, dn)

This function returns a random matrix of the specified dimension, and the random numbers are derived from samples that follow the [1,0) distribution.
Example:

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]]

2.2 randn(d0, d1, …, dn)

This function returns a random matrix of the specified dimension, and the random numbers are derived from samples that obey the (0,1) standard normal distribution. This function is similar to random.standard_normal, and the corresponding normal distribution N(mu, sigma^2) can be obtained by the following formula:
sigma * np.random.randn(…) + mu
Example:

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 ]]

2.3 randint(low[, high, size, dtype])

This function returns a matrix of random numbers from a [low, high) discrete uniform distribution. If the high parameter is not specified, the range will be limited to [0, low)
Example:

np.random.randint(0, 3, (3, 4))
[[1 0 2 0]
 [0 2 0 2]
 [2 2 2 1]]

2.4 random_integers(low[, high, size])

This function is similar to the previous one, except that it returns a random number matrix that obeys the [low, high] discrete uniform distribution. If the high parameter is not specified, the range will be limited to [1, low]
Example:

np.random.random_integers(0, 3, (3, 4))
[[2 2 0 3]
 [2 0 1 2]
 [3 3 2 0]]

Generate N uniform integers between a and b:
a + (b - a) * (np.random.random_integers(N) - 1) / (N - 1.)

2.5 random_sample([size]),random([size]),ranf([size]),sample([size])

This function returns the continuous mean floating point number distribution in the range [0.0, 1.0). If the range of the numbers to be generated is [a,b), then:
(b - a) * random_sample() + a
Example:

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]]

2.6 choice(a, size=None, replace=True, p=None)

The function returns the decimation matrix in the one-dimensional array a. If a is a number, the one-dimensional number is np.arange(a).
Example:

a = [2, 4, 6, 8, 10]
np.random.choice(a, (3, 4))
[[ 2  2  6  2]
 [ 4 10  2  8]
 [ 8 10  6  2]]

2.7 bytes(length)

Returns random bytes
Example :

np.random.bytes(5)
l�;�

Common distribution functions

1. Distribution functions included in Numpy

function specific distribution
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.

2. 函数使用

这里就是用最常用的高斯分布作为示例进行讲解,其它分的使用也是类似的。

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()

这里写图片描述

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325803507&siteId=291194637