为什么 np.random.default_rng().permutation(n) 优于原始的 np.random.permutation(n)?

Why is np.random.default_rng().permutation(n) preferred over the original np.random.permutation(n)?

Numpy documentation on np.random.permutation 建议所有新代码使用随机生成器包中的 np.random.default_rng()。我在文档中看到,Random Generator 包已经标准化了围绕 BitGenerator 与使用 Mersenne Twister 生成的各种随机分布,我对此不太熟悉。

我看到了一个缺点,过去只需一行代码即可完成简单的排列:

np.random.permutation(10)

现在变成了两行代码,这么简单的任务感觉有点别扭:

rng = np.random.default_rng()
rng.permutation(10)

一些上下文:

针对您的问题,按逻辑顺序:

And why wouldn't existing methods like np.random.permutation just wrap this new preferred method?

可能是因为 。即使 "top-level" API 不会改变,其内部结构也足以被视为兼容性中断。

Why is this new approach an improvement over the previous approach?

"By default, Generator uses bits provided by PCG64 which has better statistical properties than the legacy MT19937 used in RandomState."(source). The PCG64 文档字符串提供了更多技术细节。

Is there a good reason not to use this new method as a one-liner np.random.default_rng().permutation(10), assuming it's not being called at high volumes?

我非常同意,如果它是在模块启动时添加的,那么添加的代码行会有点笨拙。我只想指出 NumPy 文档确实在文档字符串示例中直接使用了这种形式,例如:

n = np.random.default_rng().standard_exponential((3, 8000))

细微差别在于,一个人在模块 load/import 时实例化 class,而在您的表单中它可能会稍后出现。但这应该是一个微小的差异(同样,假设它只使用一次或几次)。如果您查看 default_rng(seed) 源代码,当使用 None 调用时,它只是 returns Generator(PCG64(seed)) 在对 seed.

进行几次快速检查后

Is there an argument for switching existing code to this method?

我打算继续这个,因为我没有任何接近深度的技术知识来对算法进行很好的比较,而且因为它取决于其他一些变量,比如你是否关心使您的下游代码与旧版本的 NumPy 兼容,其中 default_rng() 根本不存在。