警告
用于生成NumPy法线的Box-Muller方法在中不再可用 Generator . 不可能使用 Generator 对于正态分布或依赖于正态分布的任何其他分布,如 Generator.gamma 或 Generator.standard_t . 如果需要位向后兼容的流,请使用 RandomState ,即 RandomState.gamma 或 RandomState.standard_t .
Generator
Generator.gamma
Generator.standard_t
RandomState
RandomState.gamma
RandomState.standard_t
遗产的快速比较 mtrand 到新的 Generator
特征
较老的等价物
笔记
Generator 需要流源,称为 BitGenerator 其中提供了一些。 RandomState 使用梅森缠绕器 MT19937 默认情况下,但也可以用任何位生成器实例化。
BitGenerator
MT19937
random
random_sample, rand
random_sample
rand
访问位生成器中的值,将其转换为 float64 在间歇期 [0.0., `` 1.0)`` . 除了 size kwarg,现在支持 dtype='d' 或 dtype='f' 和一个 out kwarg填充用户提供的数组。
float64
[0.0.,
size
dtype='d'
dtype='f'
out
还支持许多其他发行版。
integers
randint, random_integers
randint
random_integers
使用 endpoint kwarg调整 high 间隔终结点
endpoint
high
更详细地说:
从复正态分布模拟 (complex_normal )
普通、指数和gamma生成器使用256步Ziggurat方法,比NumPy的默认实现快2-10倍 standard_normal , standard_exponential 或 standard_gamma .
standard_normal
standard_exponential
standard_gamma
In [1]: from numpy.random import Generator, PCG64 In [2]: import numpy.random In [3]: rg = Generator(PCG64()) In [4]: %timeit -n 1 rg.standard_normal(100000) ...: %timeit -n 1 numpy.random.standard_normal(100000) ...: 1.48 ms +- 161 us per loop (mean +- std. dev. of 7 runs, 1 loop each) 4.12 ms +- 19.2 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
In [5]: %timeit -n 1 rg.standard_exponential(100000) ...: %timeit -n 1 numpy.random.standard_exponential(100000) ...: 725 us +- 16.6 us per loop (mean +- std. dev. of 7 runs, 1 loop each) 4.59 ms +- 6 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
In [6]: %timeit -n 1 rg.standard_gamma(3.0, 100000) ...: %timeit -n 1 numpy.random.standard_gamma(3.0, 100000) ...: 3.04 ms +- 26.6 us per loop (mean +- std. dev. of 7 runs, 1 loop each) 7.08 ms +- 25 us per loop (mean +- std. dev. of 7 runs, 1 loop each)
integers 现在是从离散均匀分布生成整数随机数的标准方法。这个 rand 和 randn 方法只能通过遗留的 RandomState . 这将取代两者 randint 不赞成的 random_integers .
randn
用于生成NumPy法线的Box-Muller方法不再可用。
所有位生成器都可以通过CTypes生成double、uint64和uint32 (ctypes )和CFFI (cffi ). 这使得这些位发生器可以在numba中使用。
ctypes
cffi
钻头发生器可通过Cython在下游项目中使用。
可选的 dtype 接受的论点 np.float32 或 np.float64 为选择分布产生单预测或双预测均匀随机变量
dtype
np.float32
np.float64
制服 (random 和 integers )
法线 (standard_normal )
标准伽马 (standard_gamma )
标准指数 (standard_exponential )
In [7]: rg = Generator(PCG64(0)) In [8]: rg.random(3, dtype='d') Out[8]: array([0.63696169, 0.26978671, 0.04097352]) In [9]: rg.random(3, dtype='f') Out[9]: array([0.07524014, 0.01652753, 0.17526722], dtype=float32)
可选的 out 参数,该参数允许为所选分发版填充现有数组
制服 (random )
使用合适的多线程生成器来填充大的数据块。
In [10]: existing = np.zeros(4) In [11]: rg.random(out=existing[:2]) Out[11]: array([0.91275558, 0.60663578]) In [12]: print(existing) [0.91275558 0.60663578 0. 0. ]
可选的 axis 方法的参数,如 choice , permutation 和 shuffle 控制对多维数组在哪个轴上执行操作的。
axis
choice
permutation
shuffle
In [13]: rg = Generator(PCG64(123456789)) In [14]: a = np.arange(12).reshape((3, 4)) In [15]: a Out[15]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]) In [16]: rg.choice(a, axis=1, size=5) Out[16]: array([[ 3, 0, 2, 3, 1], [ 7, 4, 6, 7, 5], [11, 8, 10, 11, 9]]) In [17]: rg.shuffle(a, axis=1) # Shuffle in-place In [18]: a Out[18]: array([[ 3, 1, 2, 0], [ 7, 5, 6, 4], [11, 9, 10, 8]])