NumPy 的随机采样模块 random 使用简单介绍

NumPy的API的简单介绍

NumPy 提供的random模块,提供了方便的自动生成(伪)随机数的API。

一、使用简单随机数API生成随机数组

1. random.rand(d0, d1, ..., dn)

参数:

  • d0, d1, ..., dn:int, optional,非负,返回数组的维数。

返回

  • return:ndarray, shape(d0, d1, …, dn),给定形状的数组,服从[0, 1]上均匀分布的随机值。
>>> np.random.rand(3, 4)
array([[0.78535858, 0.77997581, 0.27259261, 0.27646426],
       [0.80187218, 0.95813935, 0.87593263, 0.35781727],
       [0.50099513, 0.68346294, 0.71270203, 0.37025075]])

2. random.randn(d0, d1, ..., dn)

参数:

  • d0, d1, ..., dn:int, optional,非负,返回数组的维数。

返回

  • return:ndarray, shape(d0, d1, …, dn),给定形状的数组,服从[0, 1]上的标准正态分布的随机值。
>>> np.random.randn(3, 4)
array([[ 0.1458091 ,  2.89409095, -0.30426018,  0.861661  ],
       [-0.68992667,  0.18749737,  0.60430874, -0.18301422],
       [-1.12650247,  1.65887284, -0.66044141,  1.04108597]])

3. random.randint(low, high=None, size=None, dtype=int)

参数:

  • low:int or array_like of ints,随机分布区间的最小整数,如果参数只有一个,则区间为 [0, low);
  • high:int or array_like of ints, optional,随机分布区间的最大整数则;
  • size:int or tuple of ints, optional,给定数组的大小,默认为None,此时返回一个值;
  • dtype:dtype, optional,给定结果的数据类型,默认int;

返回

  • return:ndarray,给定形状的数组,服从[low, high]上的均匀分布的随机值。

# 只给定参数low
>>> np.random.randint(6, size=10)
array([5, 1, 5, 3, 2, 0, 4, 3, 4, 0])

# 给定low值int,high值为数组
# 生成数组的对应元素服从对应数组值组成的均匀分布区间
>>> np.random.randint(2, [4,6,8], size=(2, 3))
array([[3, 2, 2],
       [3, 3, 4]])

4. random.random_integers(low, high=None, size=None, dtype=int)

类似与 random.randint 方法,只是变为了闭区间 [low, high],参数只能为 int 类型。

5. random.random_sample(size=None)

参数:

  • size:int or tuple of ints, optional,给定数组的大小,默认为None,此时返回一个值;

返回

  • return:ndarray of floats,给定形状的数组,服从[0.0, 1.0]上的均匀分布的随机值。
>>> np.random.random_sample(size=(2, 3))
array([[0.42717173, 0.78993092, 0.38612596],
       [0.97910037, 0.21940539, 0.09285527]])

6. random.choice(a, size=None, replace=True, p=None)

参数:

  • a:int or array_like,如果是类数组,则从其中随机抽取样本,如果是 int ,从np.arange(a)中抽取样本;
  • size:int or tuple of ints, optional,给定数组的大小,默认为None,此时返回一个值;
  • replace:boolean, optional,如果为True,值可重复;
  • p:int or array_like,a中对应元素被抽取的概率;如果为None,概率均匀分布;
    返回
  • return:ndarray,生成随机样本数组;
>>> np.random.choice(5, (2, 3), replace=False, p=[0.1, 0, 0.3, 0.6, 0])
array([[3, 3, 0],
       [3, 2, 3]])

7. random.bytes(length)

参数:

  • length:int,随机字节的数量;
    返回
  • return:bytes,length长度的字节;
>>> np.random.bytes(10)
b'\xb2\xbb $\xad\xfd\xebx$\xa6'

二、常见分布数据的生成

1. random.uniform(low=0.0, high=1.0, size=None)

参数:

  • low:float or array_like of floats, optional,输出间隔的下边界,默认为0。0;
  • high:float or array_like of floats,输出间隔的上边界,默认为1.0;
  • size:int or tuple of ints, optional,输出数组的大小;

返回

  • return:ndarray or scalar,服从 1/(b-a) 分布的数组;
>>> np.random.uniform(-1,1,(2, 3))
array([[ 0.39929693, -0.81612905, -0.89239555],
       [ 0.72559234,  0.25994077,  0.7393063 ]])

>>> np.random.uniform(-1,(0, 1, 2, 3),(2, 4))
array([[-0.91230913,  0.73180765, -0.87625732,  2.83269636],
       [-0.77263192,  0.5037242 ,  0.20224601,  0.6123017 ]])

2. random.normal(loc=0.0, scale=1.0, size=None)

参数:

  • loc:float or array_like of floats,分布的均值;
  • scale:float or array_like of floats,分布的标准差;
  • size:int or tuple of ints, optional,输出输出的大小;

返回

  • return:ndarray or scalar,服从正态分布的数组;
>>> np.random.normal(0, 1,(2, 4))
array([[ 0.04656189, -1.67982886,  1.39589233, -0.84497142],
       [ 0.81400694, -0.04974231,  0.53424675, -0.80700913]])

3. random.exponential(scale=1.0, size=None)

参数:

  • scale:float or array_like of floats,参数 必须是非负;
  • size:int or tuple of ints, optional,输出尺寸;

返回

  • return:ndarray or scalar,服从指数分布的数组;
>>> np.random.exponential(size=(2, 4))
array([[2.03866542, 0.138469  , 3.65180763, 1.40330064],
       [2.48503613, 0.03843198, 0.33666064, 1.73108522]])

三、随机排列函数

1. random.shuffle(x)

参数:

  • x:ndarray or MutableSequence,数组、列表或者可变数组;

返回:

  • Returns:None;沿着多维数组的第一维洗牌数组。
>>> arr = np.random.randint(1, 10, size=(4,3))
>>> print(arr)
>>> np.random.shuffle(arr)
>>> print(arr)
[[1 7 1]
 [8 4 2]
 [9 6 7]
 [2 3 5]]
[[9 6 7]
 [2 3 5]
 [1 7 1]
 [8 4 2]]

2. random.permutation(x)

参数:

  • x:int or array_like,如果是int,随机洗牌 np.arange(x),如果是数组,将发生复制并洗牌;

返回:

  • Returns:ndarray,沿着多维数组的第一维洗牌数组,并返回。
>>> arr = np.arange(12).reshape(3, 4)
>>> print(arr)
>>> np.random.permutation(arr)
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])

四、随机种子

1. numpy.seed(seed=None)

向随机数生成器传递随机状态种子,生成全局随机种子。
同一随机种子将生成相同的随机数。

2. numpy.random.RandomState对象

只改变局部的随机种子。

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值