numpy中RandomState和种子之间的区别 [英] Difference between RandomState and seed in numpy

查看:91
本文介绍了numpy中RandomState和种子之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了文档,但是仍然很难理解

I have read the documentation but I still find difficult to understand the difference between the use of

numpy.random.RandomState(0) 

numpy.random.seed(0)

难道它们都不能确保选择随机值的过程在整个运行过程中是相同且一致的吗?

Don't they both ensure that the process through which the random values are selected is the same and consistent across runs?

推荐答案

numpy.random.seed(0)重置现有全局RandomState实例的状态,该实例是numpy.random命名空间中功能的基础.

numpy.random.seed(0) resets the state of the existing global RandomState instance that underlies the functions in the numpy.random namespace.

numpy.random.RandomState(0)返回一个新的种子RandomState实例,但否则不做任何更改.您必须使用返回的RandomState实例来获取一致的伪随机数.如果使用numpy.random命名空间中的函数,则不会获得一致的伪随机数,因为它们是从与刚创建的实例不同的RandomState实例中提取的.

numpy.random.RandomState(0) returns a new seeded RandomState instance but otherwise does not change anything. You have to use the returned RandomState instance to get consistent pseudorandom numbers. If you use the functions in the numpy.random namespace, you will not get consistent pseudorandom numbers because they are pulling from a different RandomState instance than the one you just created.

如果您关心可重复性,则最好将代码结构化以传递RandomState实例.全球状态糟透了. C.f. 始终创建相同的随机numpy数组

If you care about reproducibility, it is highly preferable to structure your code to pass around RandomState instances. Global state sucks. C.f. Consistenly create same random numpy array

这篇关于numpy中RandomState和种子之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆