[英]Should I use numpy's Random Generator?
I have a large Python code that I've been maintaining/updating/expanding since ~2014.我有一个大的 Python 代码,自 2014 年以来我一直在维护/更新/扩展。 Recently I came across numpy
's Random Number Generator Policy (2018-05) and now I'm a bit confused.最近我遇到了numpy
的Random Number Generator Policy (2018-05),现在我有点困惑。
I'm not sure what changed, and if I should upgrade my code accordingly to use the new Random Generator .我不确定发生了什么变化,以及是否应该相应地升级我的代码以使用新的Random Generator 。 For example, the Random sampling docs say:例如,随机抽样文档说:
# Do this
from numpy.random import default_rng
rng = default_rng()
vals = rng.standard_normal(10)
more_vals = rng.standard_normal(10)
# instead of this
from numpy import random
vals = random.standard_normal(10)
more_vals = random.standard_normal(10)
All my code depends on the (old?) syntax shown in the second block (ie, I don't use default_rng
but simple calls to np.random.seed()
, np.random.uniform()
, np.random.normal()
, etc), and I don't know why I should use the first block instead of the second block.我所有的代码都依赖于第二个块中显示的(旧?)语法(即,我不使用default_rng
而是对np.random.seed()
、 np.random.uniform()
、 np.random.normal()
的简单调用np.random.normal()
等),我不知道为什么我应该使用第一个块而不是第二个块。
Could someone shed some light over this please?有人可以对此有所了解吗?
1.In python2(old code) default_rng is not available. 1.在python2(旧代码)default_rng 不可用。
2.In python3(new code) both first and second blocks you mentioned will runs without an error and executed. 2.在python3(新代码)中,您提到的第一个和第二个块都将正常运行并执行。
3.In future they may drop the random.standard_normal from coming versions of python,that's why they mentioned to use of default_rng instead of random.standard_normal 3.将来他们可能会从即将发布的 python 版本中删除 random.standard_normal,这就是为什么他们提到使用 default_rng 而不是 random.standard_normal
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.