@TOC
np.random
1. np.random.rand
np.random.rand(d0, d1, ... , dn)
- 参数d0…dn为array的形状
- output:
array.shape=(d0, d1, ... , dn)
返回一个随机array,形状为给定参数的形状
array中的元素从均匀的
[0, 1)分布中采样得到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).
1 | x = np.random.rand(3,2) |
注意:np.random.random与np.random.rand作用完全一致,只有输入的参数不同,如果要生成一个(3, 2)的array
np.random.random((3, 2))np.random.rand(3, 2)
2. np.random.randn
np.random.randn(d0, d1, ... , dn)
- 参数d0…dn为array的形状
- output:
array.shape=(d0, d1, ... , dn)
返回一个随机array,形状为给定参数的形状
array中的元素从标准正态分布
N~(0, 1)中采样Return a sample (or samples) from the “standard normal” distribution.
注意:如果要从中采样,使用
sigma * np.random.randn(...) + mu
1 | np.random.randn() |
3. np.random.randint
np.random.randint(low, high=None, size=None, dtype=int)
- low:下边界(包含)
- high:上边界(不包含)
- size:数组大小
返回一个随机整型array,形状为给定参数的形状
array中的元素从
[low, high)中采样整数Return random integers from low (inclusive) to high (exclusive).
Return random integers from the “discrete uniform” distribution of the specified dtype in the “half-open” interval [low, high). If high is None (the default), then results are from [0, low).
1 | np.random.seed(1) |
4. np.random.choice
np.random.choice(a, size=None, replace=True, p=None)
- a:一维array(tuple or dic)或者int;如果是int采样集合为
np.arange即 - szie:int或者tuple,int表示采样几个值,tuple表示采样出来什么样的array,比如
tuple=(m,n,k),采样出来的结果为m * n * k的array,default=None采样一个数 - replace:是否可以重复采样。
default=True,可以 - p:array中各个数的采样概率。optional=None,等概率采样
从给定的一维数组中采样一个随机样本
Generates a random sample from a given 1-D array
1 | x = np.random.choice(5, 3) |