numpy.random模块--np.random.seed与np.random.RandomState

如上一篇文章所介绍的,numpy.random模块提供了一些高效生成各种概率分布下随机数据的函数。实际上,这些随机数是伪随机数,因为他们是由具有确定性行为的算法根据随机数生成器的随机种子生成的。通过设置相同的随机种子,可以使生成的随机数相同。

numpy.random.seed :全局随机种子

当设置相同的seed,生成的随机数相同。但是seed只能一次有效,如果调用生成随机数的函数之前没有再次设置相同的seed,则生成的随机数是不同的。

In [157]: np.random.seed(0) # 设置全局随机种子为0
In [158]: np.random.rand(5)
Out[158]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])

In [160]: np.random.seed(0) # 再次设置相同的全局随机种子,生成的随机数相同
In [161]: np.random.rand(5)
Out[161]: array([0.5488135 , 0.71518937, 0.60276338, 0.54488318, 0.4236548 ])

In [159]: np.random.rand(5) # 不设置随机种子,两次生成的随机数不同
Out[159]: array([0.64589411, 0.43758721, 0.891773  , 0.96366276, 0.38344152])

numpy.random.RandomState: 局部随机种子

通过调用numpy.random.RandomState可以生成一个随机数生成器,通过该生成器调用生成随机数的函数,就可以生成随机数。

In [2]: rng = np.random.RandomState(1)

In [3]: rng.rand(4)
Out[3]: array([4.17022005e-01, 7.20324493e-01, 1.14374817e-04, 3.02332573e-01])

通过传递相同的随机种子作为numpy.random.RandomState()的参数,就可以生成相同的随机数。与全局随机种子类似,局部随机种子也是仅一次有效。必须创建一次随机数生成器,再通过生成器调用函数,只有这样才能保证生成的随机数相同。

In [2]: rng = np.random.RandomState(1)

In [4]: x = rng.rand(4) 
In [5]: x
Out[5]: array([0.14675589, 0.09233859, 0.18626021, 0.34556073])

In [6]: y = rng.rand(4) # 通过生成器两次调用rand()函数生成的随机数组是不同的。
In [7]: y
Out[7]: array([0.39676747, 0.53881673, 0.41919451, 0.6852195 ])
In [8]: rng = np.random.RandomState(1)
In [9]: x = rng.rand(4)
In [10]: x
Out[10]: array([4.17022005e-01, 7.20324493e-01, 1.14374817e-04, 3.02332573e-01])

In [11]: rng = np.random.RandomState(1) # 写一次生成器,调用一次rand()函数才可以保证生成的随机数组相同。
In [12]: y = rng.rand(4)
In [13]: y
Out[13]: array([4.17022005e-01, 7.20324493e-01, 1.14374817e-04, 3.02332573e-01])

局部随机种子的独立性

  • np.random.seednp.random.RandomState的相同点:可以通过设置相同的随机种子生成相同的随机数
  • np.random.seednp.random.RandomState的不同点:np.random.RandomState具有独立性,即在np.random.seed设置好的情况下,再设置np.random.RandomState,不会影响np.random.seed随机数的生成。
In [20]: np.random.seed(0) # 设置全局随机种子

In [21]: for i in range(3):
    ...:     print('global {}th: '.format(i), np.random.rand(3))
    ...:
global 0th:  [0.5488135  0.71518937 0.60276338]
global 1th:  [0.54488318 0.4236548  0.64589411]
global 2th:  [0.43758721 0.891773   0.96366276]

In [22]: np.random.seed(0) # 设置相同的全局随机种子
In [23]: rng = np.random.RandomState(1) # 设置局部随机种子

In [25]: for i in range(3):
    ...:     print('global {}th: '.format(i), np.random.rand(3))
    ...:     print('local {}th: '.format(i), rng.rand(3))
    ...:
global 0th:  [0.5488135  0.71518937 0.60276338]
local 0th:  [4.17022005e-01 7.20324493e-01 1.14374817e-04]
global 1th:  [0.54488318 0.4236548  0.64589411]
local 1th:  [0.30233257 0.14675589 0.09233859]
global 2th:  [0.43758721 0.891773   0.96366276]
local 2th:  [0.18626021 0.34556073 0.39676747]
# 设置局部随机种子之后,再设置相同的全局随机种子,生成的随机数仍然相同。两者互不影响。
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值