eri*_*ric 7 python numpy random-seed
我经常使用它numpy.random.seed()来提高结果的可重复性。我还发现当书籍/教程使用它时它很有帮助(例如,np.random.seed(42)),以便人们可以更轻松地重现事物。
目前的文档称random.seed()不鼓励这样做,并且是一个遗留功能,但没有任何解释。我想知道为什么:
random.seed()
Reseed a legacy MT19937 BitGenerator
This is a convenience, legacy function.
The best practice is to not reseed a BitGenerator,
rather to recreate a new one. This method is here
for legacy reasons.
Run Code Online (Sandbox Code Playgroud)
请注意,文档继续说道:
# This example demonstrates best practice.
from numpy.random import MT19937
from numpy.random import RandomState, SeedSequence
rs = RandomState(MT19937(SeedSequence(123456789)))
# Later, you want to restart the stream
rs = RandomState(MT19937(SeedSequence(987654321)))
Run Code Online (Sandbox Code Playgroud)
我不确定为什么这是最佳实践。为什么这比使用更好random.seed()?特别是,当您在教授一个概念并希望每次都重现相同的结果并希望保持代码简单时?至少在表面上,random.seed()还调用MT19937BitGenerator (尽管从文档来看它是一个遗留位生成器,所以它们不一样)。