美文网首页
常用numpy的random模块

常用numpy的random模块

作者: AWRL | 来源:发表于2019-01-11 15:46 被阅读0次

    本文为个人学习总结,如有不当之处,还望留言

    我们经常在数据分析、数据挖掘、机器学习等场景要使用到随机数,也因为np.random模块功能太多,所以在这里把常见的几个做个简单的汇总。

    首先没有numpy的童鞋需先安装

    pip install numpy
    

    然后导包

    import numpy as np
    

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

    • 作用:生成[0, 1)之间的指定形状的随机浮点数
    • 参数:d0, d1, ..., dn,int类型,如不写则返回单个随机数
    • 返回:ndarray类型,形状(d0, d1, ..., dn)
    >>> np.random.rand() # 没有参数则直接生成一个[0,1)区间的随机数
    0.1065982531337718
    >>> np.random.rand(3) # 只有一个参数则生成n*1个随机数
    array([0.24640203, 0.81910232, 0.79941588])
    >>> np.random.rand(2, 3, 4) # 有多个参数,则生成对应形状随机数,如本例生成shape为2*3*4的随机数
    array([[[0.73222489, 0.80656115, 0.65878337, 0.69227656],
            [0.84919565, 0.24966801, 0.48942496, 0.22120944],
            [0.98766801, 0.94405934, 0.03942681, 0.70557517]],
    
           [[0.92524832, 0.18057535, 0.56794523, 0.9154883 ],
            [0.03394598, 0.69742027, 0.29734901, 0.9243962 ],
            [0.97105825, 0.94426649, 0.47421422, 0.86204265]]])
    

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

    • 作用:生成指定形状,服从标准正态分布(均值为0,标准差为1)的随机数
    • 参数:d0, d1, ..., dn,int型,如不写则返回单个标准正态分布实例
    • 返回:ndarray类型,形状(d0, d1, ..., dn)
    >>> np.random.randn() # 没有参数则直接随机生成一个标准正态分布实例
    0.4125703966441225
    >>> np.random.randn(3) # 只有一个参数则生成n*1的服从标准正态分布的随机数
    array([-1.9296846 , -0.65058484,  1.04354591])
    >>> np.random.randn(2, 3, 4) # 有多个参数,则生成对应形状标准正态分布随机数,如本例生成shape为2*3*4的服从标准正态分布的随机数
    array([[[ 0.86776502,  0.28303221, -1.34946357,  0.58550346],
            [ 0.71339959,  0.04701721,  1.22317095, -0.68343179],
            [-0.50985758, -2.48461158,  0.91982751,  0.31386196]],
    
           [[-1.68182033, -1.758599  , -0.87263063,  0.79365794],
            [-0.30036049, -0.80478692, -1.00611589, -1.05091196],
            [-1.9336783 , -1.20739583, -1.07057808,  0.68262324]]])
    

    3、np.random.randint(low, high=None, size=None, dtype='l')

    • 作用:生成指定区间[low, high)的随机整数
    • 参数:
      -- low: 最小值,int型
      -- high: 最大值,int型
      -- size: 数组维度,int型或由int构成的tuple型
      -- dtype: 数据类型,默认为np.int
    • 返回:int型或者由int构成的ndarray
    >>> np.random.randint(5) # 只有一个参数,默认生成一个[0, 5)的随机整数
    3
    >>> np.random.randint(2, 5) # 两个参数,生成一个在[2, 5)的随机整数
    4
    >>> np.random.randint(3, size=5) # 生成5个[0, 3)的随机整数
    array([1, 0, 1, 2, 2])
    >>> np.random.randint(-3, 5, size=(3, 4)) # 在[-3, 5)区间上,生成shape为3*4的随机整数
    array([[ 3,  1, -1, -3],
           [ 2,  3,  3,  4],
           [-1, -1, -2,  3]])
    

    4、np.random.random_integers(low, high=None, size=None)

    • 作用:生成指定区间[low, high]的随机整数
    • 参数:同randint
    • 返回:同randint
    • 说明:用法和randint相似,系统弃用警告,建议使用randint
      -- 不同之处:全闭区间;只有一个参数时,区间为[1, low]

    DeprecationWarning: This function is deprecated. Please call randint() instead

    >>> np.random.rand_integers(2) # 生成[1,2]区间的一个随机数
    1
    

    5、np.random.random(size=None)

    • 作用:生成[0,1)区间指定size的随机浮点数
    • 参数:size,数组维度,int型或由int构成的tuple型
    • 返回:float型或由float构成的ndarray型
    >>> np.random.random() # 没有参数,直接生成一个[0, 1)区间随机浮点数
    0.15893828327675652
    >>> np.random.random(3) # 一个参数,生成[0, 1)区间shape为3*1的随机浮点数
    array([0.77282278, 0.23005368, 0.13133513])
    >>> np.random.random(size=(3, 4)) # tuple型参数,生成[0, 1)区间shape为3*4的随机浮点数
    array([[0.86805432, 0.96323665, 0.03130231, 0.73127007],
           [0.90952451, 0.85736009, 0.96452047, 0.76009408],
           [0.44229876, 0.31195199, 0.32074274, 0.72442267]])
    

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

    • 作用:从给定的一维数组中生成随机数
    • 参数:
      -- a: 一维数组或int型
      -- size: 数组维度,int型或由int构成的tuple型
      -- replace: 布尔型,False时生成的随机数无重复
      -- p: 为a的数据设置概率,不设置时默认为均匀分布
    • 返回:单个样本或多个样本构成的ndarray类型
    >>> np.random.choice([1, 3, 5, 7]) # 只有一个参数a,从a中随机选一个
    7
    >>> np.random.choice([1, 3, 5, 7], size=2) # 指定size,从a中随机选2个
    array([1, 7])
    >>> np.random.choice([1, 3, 5, 7], size=(2, 3)) # size为tuple,从a中随机选2*3个
    array([[5, 3, 3],
           [1, 1, 3]])
    >>> np.random.choice([1, 2, 3, 4, 5, 6, 7, 8, 9], size=(2, 3), replace=False) # replace设置为false时,生成的随机数不重复
    array([[7, 2, 6],
           [4, 1, 5]])
    >>> np.random.choice([1, 2, 3, 4], size=2, p=(0.1, 0.2, 0.3, 0.4)) # 设置p,根据p的权重随机挑选
    array([4, 4])
    

    7、np.random.permutation(x)

    • 作用:随机打乱数据序列
    • 参数:x,int型或array型
    • 返回:ndarray,一个打乱后的数据
    >>> np.random.permutation(4) # 一个整数,把[0, 4)的整数打乱顺序
    array([2, 1, 3, 0])
    >>> np.random.permutation([1, 3, 5, 7, 9, 11]) # 一维数组,打乱一维数组顺序
    array([ 5,  3, 11,  9,  7,  1])
    >>> x = np.arange(9).reshape((3, 3))
    >>> x =
    array([[0, 1, 2],
           [3, 4, 5],
           [6, 7, 8]])
    >>> np.random.permutation(x) # 多维数组,打乱多维数组的顺序
    array([[6, 7, 8],
           [3, 4, 5],
           [0, 1, 2]])
    

    8、np.random.seed(seed=None)

    • 作用:初始化伪随机数发生器(生成的随机数下次还要用,则可用随机数种子)
    • 参数:seed,int型或一维array型
    >>> for i in range(5):
    >>>     print(np.random.rand(3)) 
    [0.0675444  0.73276212 0.60205068]
    [0.38711645 0.81037221 0.95704267]
    [0.74261574 0.30360743 0.64652647]
    [0.18686297 0.54379681 0.45931029]
    [0.60036382 0.76657129 0.98785048]
    # 我们可以看到当不加随机数种子时生成的5次随机数都不一样
    >>> for i in range(5):
    >>>     np.random.seed(42) # 参数42,个人理解为42空间
    >>>     print(np.random.rand(3))
    [0.37454012 0.95071431 0.73199394]
    [0.37454012 0.95071431 0.73199394]
    [0.37454012 0.95071431 0.73199394]
    [0.37454012 0.95071431 0.73199394]
    [0.37454012 0.95071431 0.73199394]
    # 当添加随机数种子,5次随机生成的数都一样
    

    更多详细用法可以参照官方文档:https://www.numpy.org/devdocs/reference/routines.random.html

    结语:以上为本人对常用random模块的小结,希望能帮到大家。

    相关文章

      网友评论

          本文标题:常用numpy的random模块

          本文链接:https://www.haomeiwen.com/subject/fraarqtx.html