numpy.random.rand(d0, d1, …, dn)
Random values in a given shape.
Create an array of the given shape and populate it with random samples from a uniform distribution(均匀分布) over [0, 1).
'''
Parameters: d0, d1, ..., dn : int, optional
The dimensions of the returned array, should all be positive. If no argument is given a single Python float is returned.
Returns: out : ndarray, shape (d0, d1, ..., dn)
Random values.
'''
import numpy as np
a = np.random.rand() # a = 0.06127240321484284
b = np.random.rand(1) # b = [ 0.41584465], b.shape=(1,)
c = np.random.rand(2) # c = [ 0.77530365 0.69171108]
d = np.random.rand(2,1)
'''
d =
[[ 0.96921955]
[ 0.06063684]]
'''
e = np.random.rand(1,2) # e = [[ 0.83303266 0.15216985]]
numpy.random.randn(d0, d1, …, dn)
Return a sample (or samples) from the “standard normal” distribution(“标准正态”分布).
If positive, int_like or int-convertible arguments are provided, randn generates an array of shape (d0, d1, …, dn), filled with random floats sampled from a univariate “normal” (Gaussian) distribution of mean 0 and variance 1 (if any of the d_i are floats, they are first converted to integers by truncation). A single float randomly sampled from the distribution is returned if no argument is provided.
This is a convenience function(便利函数). If you want an interface that takes a tuple as the first argument, use numpy.random.standard_normal instead.
'''
Parameters: d0, d1, ..., dn : int, optional
The dimensions of the returned array, should be all positive.If no argument is given a single Python float is returned.
Returns:Z : ndarray or float
A (d0, d1, ..., dn)-shaped array of floating-point samples from the standard normal distribution,or a single such float if no parameters were supplied.
Notes:
For random samples from N(\mu, \sigma^2), use:
sigma * np.random.randn(...) + mu
'''
import numpy as np
a = np.random.randn() # a=-1.3911873244822348
b = np.random.randn(1) # b=[-0.19737295]
c = np.random.randn(2) # c=[ 0.94147094 -1.41735681]
d = np.random.randn(2,1)
'''
d=
[[ 0.56281246]
[ 1.33878953]]
'''
e = np.random.randn(1,2) # e=[[-0.50389743 -1.69531897]]
Notes:
For random samples from N(\mu, \sigma^2), use:
sigma * np.random.randn(...) + mu
Two-by-four array of samples from N(3, 6.25):
2.5 * np.random.randn(2, 4) + 3
array([[-4.49401501, 4.00950034, -1.81814867, 7.29718677],
[ 0.39924804, 4.68456316, 4.99394529, 4.84057254]])