numpy中RandomState和种子之间的区别 [英] Difference between RandomState and seed in numpy
问题描述
我已经阅读了文档,但是仍然很难理解
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屋!