新增功能或不同之处#
NumPy 1.17.0 引入了 Generator 作为 legacy RandomState 的改进替代品.这是一个对两种实现的快速比较.
特性 |
旧的等效项 |
注释 |
|
||
访问 BitGenerator 中的值,将其转换为 还支持许多其他发行版. |
||
使用 |
正态,指数和伽马生成器使用 256 步 Ziggurat 方法,该方法比 NumPy 在
standard_normal,standard_exponential或standard_gamma中的默认实现快 2-10 倍.由于算法的更改,因此无法使用这些分布或依赖于它们的任何分布方法的Generator重现确切的随机值.
In [1]: import numpy.random
In [2]: rng = np.random.default_rng()
In [3]: %timeit -n 1 rng.standard_normal(100000)
...: %timeit -n 1 numpy.random.standard_normal(100000)
...:
The slowest run took 5.41 times longer than the fastest. This could mean that an intermediate result is being cached.
2.43 ms +- 2.15 ms per loop (mean +- std. dev. of 7 runs, 1 loop each)
3.34 ms +- 163 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
In [4]: %timeit -n 1 rng.standard_exponential(100000)
...: %timeit -n 1 numpy.random.standard_exponential(100000)
...:
963 us +- 85.3 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
2.81 ms +- 636 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
In [5]: %timeit -n 1 rng.standard_gamma(3.0, 100000)
...: %timeit -n 1 numpy.random.standard_gamma(3.0, 100000)
...:
3.46 ms +- 383 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
6.33 ms +- 536 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
integers现在是从离散均匀分布生成整数随机数的规范方法.这取代了randint和已弃用的random_integers.rand和randn方法仅通过旧版RandomState提供.Generator.random现在是生成浮点随机数的规范方法,它取代了RandomState.random_sample,sample和ranf,所有这些都是别名.这与 Python 的random.random一致.所有位生成器都可以通过 CTypes (
ctypes) 和 CFFI (cffi) 生成双精度型,uint64 和 uint32.这允许这些位生成器在 numba 中使用.位生成器可以通过 Cython 在下游项目中使用.
所有位生成器都使用
SeedSequence来 convert seed integers to initialized states .可选的
dtype参数,它接受np.float32或np.float64,以生成单精度或双精度均匀随机变量,用于选择分布.integers接受一个dtype参数,该参数具有任何有符号或无符号整数 dtype.正态分布 (
standard_normal)标准伽马分布 (
standard_gamma)标准指数分布 (
standard_exponential)
In [6]: rng = np.random.default_rng()
In [7]: rng.random(3, dtype=np.float64)
Out[7]: array([0.40436966, 0.57953545, 0.38496263])
In [8]: rng.random(3, dtype=np.float32)
Out[8]: array([0.2718469 , 0.15777564, 0.7979569 ], dtype=float32)
In [9]: rng.integers(0, 256, size=3, dtype=np.uint8)
Out[9]: array([199, 169, 22], dtype=uint8)
可选的
out参数,允许填充现有数组以用于选择分布均匀分布 (
random)正态分布 (
standard_normal)标准伽马分布 (
standard_gamma)标准指数分布 (
standard_exponential)
这允许使用多线程以并行方式使用合适的 BitGenerators 分块填充大型数组.
In [10]: rng = np.random.default_rng()
In [11]: existing = np.zeros(4)
In [12]: rng.random(out=existing[:2])
Out[12]: array([0.70319608, 0.97012694])
In [13]: print(existing)
[0.70319608 0.97012694 0. 0. ]
可选的
axis参数,用于像choice,permutation和shuffle这样的方法,用于控制对多维数组执行操作的轴.
In [14]: rng = np.random.default_rng()
In [15]: a = np.arange(12).reshape((3, 4))
In [16]: a
Out[16]:
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]])
In [17]: rng.choice(a, axis=1, size=5)
Out[17]:
array([[ 0, 0, 2, 3, 2],
[ 4, 4, 6, 7, 6],
[ 8, 8, 10, 11, 10]])
In [18]: rng.shuffle(a, axis=1) # Shuffle in-place
In [19]: a
Out[19]:
array([[ 2, 3, 1, 0],
[ 6, 7, 5, 4],
[10, 11, 9, 8]])
添加了一个从复正态分布中采样的方法 ( complex_normal )