转《为什么你用不好Numpy的random函数?》

转自为什么你用不好Numpy的random函数?

Table of Contents


在python数据分析的学习和应用过程中,经常需要用到numpy的随机函数,由于随机函数random的功能比较多,经常会混淆或记不住,下面我们一起来汇总学习下。

1、numpy.random.rand()

numpy.random.rand(d0,d1,…,dn)

  • rand函数根据给定维度生成[0,1)之间的数据,包含0,不包含1
  • dn表格每个维度
  • 返回值为指定维度的array
In [1]: import numpy as np

In [2]: np.random.rand(4,2)
Out[2]:
array([[0.03543649, 0.65699022],
       [0.9640137 , 0.41203671],
       [0.0274156 , 0.39526102],
       [0.3824676 , 0.45293477]])
In [3]: np.random.rand(4,3,2) # shape: 4*3*2
Out[3]:
array([[[0.28132306, 0.55141521],
        [0.54143807, 0.70934416],
        [0.32648501, 0.09187119]],

       [[0.95055997, 0.23998789],
        [0.01906987, 0.60294769],
        [0.61123351, 0.73415631]],

       [[0.58635931, 0.02097799],
        [0.19919756, 0.80204155],
        [0.60294895, 0.52479401]],

       [[0.89149168, 0.4630619 ],
        [0.96669213, 0.13155044],
        [0.78773533, 0.86499175]]])

2、numpy.random.randn()

numpy.random.randn(d0,d1,…,dn)

  • randn函数返回一个或一组样本,具有标准正态分布。
  • dn表格每个维度
  • 返回值为指定维度的array
In [4]: np.random.randn() # 当没有参数时,返回单个数据
Out[4]: 0.6788858950501044
In [5]: np.random.randn(2,4)
Out[5]:
array([[ 1.32778139, -1.13259764,  0.45843893, -0.64685327],
       [-0.82564083, -1.28328157, -2.01779807,  1.13130852]])
In [6]: np.random.randn(4,3,2)
Out[6]:
array([[[-0.69405862,  1.35067816],
        [-0.06843673,  0.94591779],
        [-0.35769003, -1.12850492]],

       [[-0.4138253 ,  1.29171803],
        [ 1.14909845, -1.06347911],
        [ 0.70011367,  0.08417853]],

       [[-0.01840155, -0.10676455],
        [ 0.03287281, -0.01085714],
        [ 1.23423707,  0.37175353]],

       [[-0.9376414 , -1.25016235],
        [ 0.53927611,  0.16836484],
        [-0.41082093,  1.87387039]]])

3、numpy.random.randint()

3.1 numpy.random.randint()

numpy.random.randint(low, high=None, size=None, dtype='l')

  • 返回随机整数,范围区间为[low,high),包含low,不包含high
  • 参数:low为最小值,high为最大值,size为数组维度大小,dtype为数据类型,默认的数据类型是np.int
  • high没有填写时,默认生成随机数的范围是[0,low)
In [7]: np.random.randint(1,size=5) # 返回[0,1)之间的整数,所以只有0
Out[7]: array([0, 0, 0, 0, 0])
In [8]: np.random.randint(1,5) # 返回1个[1,5)时间的随机整数
Out[8]: 2
In [9]: np.random.randint(-5, 5, size=(2,2))
Out[9]:
array([[3, 1],
       [3, 0]])
3.2 numpy.random.random_integers

numpy.random.random_integers(low, high=None, size=None)

  • 返回随机整数,范围区间为[low,high],包含low和high
  • 参数:low为最小值,high为最大值,size为数组维度大小
  • high没有填写时,默认生成随机数的范围是[1,low]
  • 该函数在最新的numpy版本中已被替代,建议使用randint函数
In [11]: np.random.random_integers(1, size=5)
C:\Users\shexuan\AppData\Local\Continuum\anaconda3\Scripts\ipython:1: DeprecationWarning: This function is deprecated. Please call randint(1, 1 + 1) instead
Out[11]: array([1, 1, 1, 1, 1])
4、 生成[0,1)之间的浮点数
  • numpy.random.random_sample(size=None)
  • numpy.random.random(size=None)
  • numpy.random.ranf(size=None)
  • numpy.random.sample(size=None)
    这四个函数其实都是一样的,其余的三个只是numpy.random.random_sample的重命名。
In [12]: np.random.random
Out[12]: <function RandomState.random_sample>

In [13]: np.random.ranf
Out[13]: <function RandomState.random_sample>

In [14]: np.random.sample
Out[14]: <function RandomState.random_sample>

In [15]: np.random.random_sample is np.random.random
Out[15]: True

In [16]: np.random.random_sample is np.random.ranf
Out[16]: True

In [17]: np.random.random_sample is np.random.sample
Out[17]: True

In [18]: np.random.random(size=(2,2))
Out[18]:
array([[0.31486874, 0.37654112],
       [0.05517854, 0.42868528]])
5、numpy.random.choice()

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

  • 从给定的一维数组中生成随机数
  • 参数: a为一维数组类似数据或整数;size为数组维度;p为数组中的数据出现的概率
  • a为整数时,对应的一维数组为np.arange(a)
In [20]: np.random.choice(5, 3, replace=False) # 当replace为False时,生成的随机数不能有重复的数值
Out[20]: array([2, 0, 1])

In [21]: demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
    ...: np.random.choice(demo_list,size=(3,3))
    ...:
    ...:
Out[21]:
array([['iphone', 'moto', 'sansumg'],
       ['xiaomi', 'xiaomi', 'lenovo'],
       ['iphone', 'sansumg', 'xiaomi']], dtype='<U7')

# 参数p的长度与参数a的长度需要一致;
# 参数p为概率,p里的数据之和应为1
In [24]: demo_list = ['lenovo', 'sansumg','moto','xiaomi', 'iphone']
    ...: np.random.choice(demo_list,size=(3,3), p=[0.1,0.6,0.1,0.1,0.1])
    ...:
    ...:
Out[24]:
array([['lenovo', 'sansumg', 'sansumg'],
       ['sansumg', 'sansumg', 'sansumg'],
       ['sansumg', 'lenovo', 'sansumg']], dtype='<U7')

6、numpy.random.seed()

  • np.random.seed()的作用:使得随机数据可预测。
  • 当我们设置相同的seed,每次生成的随机数相同。如果不设置seed,则每次会生成不同的随机数
In [25]: np.random.seed(0)

In [26]: np.random.rand(5)
Out[26]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])

In [27]: np.random.rand(5)
Out[27]: array([0.64589411, 0.43758721, 0.891773  , 0.96366276, 0.38344152])

In [28]: np.random.seed(0)

In [29]: np.random.rand(5)
Out[29]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])

猜你喜欢

转载自blog.csdn.net/weixin_34337265/article/details/86899696