numpy.random对python内置numpy做了补充,可用于生成多种概率分布的函数
import numpy as np
随机数
1.rand
均匀分布的随机样本值
>>> np.random.rand(2, 4)
array([[0.40358731, 0.31135908, 0.18655176, 0.13153125],
[0.38510025, 0.94966924, 0.35708047, 0.2982288]])
>>> np.random.rand()
0.2828292266430771
2.randint
限定范围内[start, end)的随机整数
>>> np.random.randint(1)
0
>>> np.random.randint(5.1, 6.9)
5 # start,end参数为小数时,向下取整
>>> np.random.randint(2, size=5)
array([1, 1, 0, 0, 1]) # size参数指定返回的数组维度
>>> np.random.randint(0,9, size=(2,5))
array([[4, 1, 7, 2, 3],
[3, 6, 7, 2, 6]])
3.random_integers
限定范围内[start, end]的随机整数,用法同randint
4.choice(a, size=None, replace=True, p=None)
从指定数组a中(必须一维)中按照指定概率p(必须与a保持相同长度)选出指定数量size的数字,数字不允许相同时,replace指定为False
>>> np.random.choice(5)
2 # 等同于np.random.randint(5),即[0,5)
>>> np.random.choice(5, 3)
array([3, 4, 3])
>>> np.random.choice(3, 5, replace=False) # 从[0, 3)中选取5个数,且不允许重复,3个数字无法选出5个不重复,所以会报错
ValueError: Cannot take a larger sample than population when 'replace=False'
>>> np.random.choice(4, 10, p=[0,0.2,0.5,0.3])
array([2, 3, 1, 2, 2, 1, 1, 2, 3, 2]) # p与a长度不一致会报错,p中的概率加一起必须等于1,否则会报错
5.bytes
返回指定数量的随机字节
>>> np.random.bytes(0)
b''
>>> np.random.bytes(2)
b'r\xab'
>>> np.random.bytes(5)
b'\x84\xb7?\xf2t'
>>> np.random.bytes(9)
b'9\x8c|\x1a\xe2\x92\xdf\xd8\xd6'
排列
1.shuffle
>>> arr = np.arange(5)
>>> np.random.shuffle(arr)
>>> arr
array([5, 3, 1, 4, 0, 9, 7, 2, 6, 8])
>>> arr = np.arange(9).reshape((3, 3))
>>> np.random.shuffle(arr)
>>> arr
array([[3, 4, 5],
[6, 7, 8],
[0, 1, 2]])
>>>#只打乱最外层排列,内层数组不受影响
>>> arr = [1, 4, 9, 12, 7]
>>> np.random.shuffle(arr)
>>> arr
array([7, 1, 9, 4, 12])
2.permutation
>>> np.random.permutation(10)
array([1, 7, 4, 3, 0, 9, 2, 5, 8, 6])
>>> np.random.permutation([1, 2, 5, 7, 6, 13])
array([ 7, 1, 2, 13, 5, 6])
>>> np.random.permutation(np.arange(9).reshape((3, 3)) )
array([[6, 7, 8],
[0, 1, 2],
[3, 4, 5]])
注:两者区别在于,shuffle直接打乱原数组,无返回值;permutation返回一个已打乱的新数组,原数组不受影响。
分布
1.randn
返回一个标准正态分布的随机数组。以0为均值、以1为标准差,记为N(0,1)
>>> np.random.randn()
0.058260810704149645
>>> np.random.randn(4)
array([ 0.57949793, -0.72707183, 0.46068701, -0.42501938])
>>> np.random.randn(2, 5)
array([[-0.34831417, -2.24118827, -0.89462094, 1.0812918 , -1.74175702],
[1.4792852 , -0.11441693, 0.68281242, -0.21708399, 1.57633102]])
>>> np.random.randn(2, 3, 5)
array([[[ 0.58851749, 1.55607863, -0.17361385, -1.81219197, 1.56844646],
[-1.2106641 , -0.38884469, 1.04644169, 0.71198851, -0.95614175],
[-0.36446878, -0.56629952, -0.10483049, 0.61600023, 0.83915336]],
[[ 0.22838538, -0.35664594, 1.73476651, 1.09740862, -0.39602795],
[-0.02949415, 0.98044707, -0.34829993, -0.48268618, -2.10986081],
[-1.22591237, -0.65165683, -0.64373846, -0.4957314 , 1.67725648]]])
2.binomial(n, p, size=None)
返回一个二项分布的随机数组,数组中的每个元素代表相应的一次实验(一次实验进行n次,成功率为p)中成功的次数
>>> n, p = 2, 0.5
#抛硬币,两面,概率都为0.5
>>> np.random.binomial(n, p, size=2000)
array([2, 1, 1, ..., 1, 2, 1])
>>> sum(np.random.binomial(n, p, size=2000)==2)/2000
0.2488 # 连续两次结果相同的概率,每次运行结果不同,都近似0.25
>>> sum(np.random.binomial(n, p, size=2000)==1)/2000
0.50065 # 只抛一次结果为正的概率,每次运行结果不同,都近似0.5
3.normal(loc=0.0, scale=1.0, size=None)
高斯分布,loc为概率分布的均值,对应着整个分布的中心center,scale为概率分布的标准差,对应于分布的宽度,size为输出的shape
所谓标准正态分布np.random.randn(size),对应于np.random.normal(loc=0, scale=1, size)
4.beta
Beta分布
5.uniform
[start, end)均匀分布的数组,默认[0,1)
>>> np.random.uniform()
0.8653566629528624
>>> np.random.uniform(3)
1.3655642233463572
>>> np.random.uniform(3, 7)
4.291530071774297
>>> np.random.uniform(3, 7, 5)
array([3.78587695, 3.80059932, 3.2971499 , 6.06415251, 5.08156897])
网友评论