随机发生器

这个 Generator 提供对广泛分布的访问,并作为 RandomState . 两者的主要区别在于 Generator 依赖于一个额外的位生成器来管理状态并生成随机位,然后将这些随机位从有用的分布转换为随机值。所使用的默认位生成器 GeneratorPCG64 . 位生成器可以通过将实例化的位生成器传递给 Generator .

numpy.random.default_rng()

用默认的位生成器(PCG64)构造一个新的生成器。

参数
seed{None, int, array_like[ints], SeedSequence, BitGenerator, Generator}, optional

初始化的种子 BitGenerator . 如果没有,那么新的,不可预测的熵将从操作系统中提取。如果 intarray_like[ints] 如果通过了,那么它将被传递给 SeedSequence 导出初始值 BitGenerator 州。你也可以通过 SeedSequence 实例。此外,当通过 BitGenerator ,它将由 Generator . 如果通过了 Generator ,则会原封不动地返回。

返回
发电机

初始化的生成器对象。

笔记

如果 seed 不是一个 BitGenerator 或A Generator ,一个新的 BitGenerator 已实例化。此函数不管理默认全局实例。

实例

default_rng 是随机数类的建议构造函数 Generator . 这里有几种方法可以使用 default_rng 以及 Generator 班级。

这里我们使用 default_rng 要生成随机浮点:

>>> import numpy as np
>>> rng = np.random.default_rng(12345)
>>> print(rng)
Generator(PCG64)
>>> rfloat = rng.random()
>>> rfloat
0.22733602246716966
>>> type(rfloat)
<class 'float'>

这里我们使用 default_rng 要生成3个介于0(含)和10(不含)之间的随机整数,请执行以下操作:

>>> import numpy as np
>>> rng = np.random.default_rng(12345)
>>> rints = rng.integers(low=0, high=10, size=3)
>>> rints
array([6, 2, 7])
>>> type(rints[0])
<class 'numpy.int64'>

在这里,我们指定了一个种子,以便得到可重复的结果:

>>> import numpy as np
>>> rng = np.random.default_rng(seed=42)
>>> print(rng)
Generator(PCG64)
>>> arr1 = rng.random((3, 3))
>>> arr1
array([[0.77395605, 0.43887844, 0.85859792],
       [0.69736803, 0.09417735, 0.97562235],
       [0.7611397 , 0.78606431, 0.12811363]])

如果退出并重新启动Python解释器,我们将看到再次生成相同的随机数:

>>> import numpy as np
>>> rng = np.random.default_rng(seed=42)
>>> arr2 = rng.random((3, 3))
>>> arr2
array([[0.77395605, 0.43887844, 0.85859792],
       [0.69736803, 0.09417735, 0.97562235],
       [0.7611397 , 0.78606431, 0.12811363]])
class numpy.random.Generator(bit_generator)

比特发生器的容器。

Generator 显示从各种概率分布中生成随机数的许多方法。除了特定于分布的参数外,每个方法都采用关键字参数 size 默认为 None .如果 sizeNone ,然后生成并返回单个值。如果 size 是一个整数,然后返回一个包含生成值的一维数组。如果 size 是一个元组,然后填充并返回具有该形状的数组。

函数 numpy.random.default_rng 将实例化 Generator 以numpy的默认值 BitGenerator .

无兼容性保证

Generator 不提供版本兼容性保证。尤其是,随着更好的算法的发展,比特流可能会改变。

参数
bit_generatorBitGenerator

比特发生器用作核心发生器。

参见

default_rng

推荐建造商 Generator .

笔记

Python stdlib模块 random 包含伪随机数生成器,其中包含许多与中提供的方法类似的方法 Generator . 它使用Mersenne Twister,这个位生成器可以使用 MT19937 . Generator 除了 NumPy 的意识之外,它的优点是它提供了更多的概率分布供选择。

实例

>>> from numpy.random import Generator, PCG64
>>> rg = Generator(PCG64())
>>> rg.standard_normal()
-0.203  # random

访问BitGenerator

bit_generator 

获取生成器使用的位生成器实例

简单随机数据

integers (低) [, high, size, dtype, endpoint] )

从返回随机整数 low (包括) high (独占),或者如果endpoint=True, low (包括) high (包括在内)。

random \ [size, dtype, out] )

返回半开区间内的随机浮点数[0.0,1.0]。

choice (a) [, size, replace, p, axis, shuffle] )

从给定的一维数组生成随机样本

bytes (长度)

返回随机字节。

排列

随机排列序列的方法有

shuffle (x) [, axis] )

通过改变序列的内容来修改序列。

permutation (x) [, axis] )

随机排列序列,或返回排列范围。

permuted (x) [, axis, out] )

随机排列 x 沿轴线 axis .

下表总结了这些方法的行为。

方法

复制/就位

轴线处理

随机播放

就位

好像1d

置换

复制

好像1d

置换的

任意(使用“out”表示“in-place”)

轴独立

以下小节提供了有关这些差异的更多详细信息。

就地复制与

两者的主要区别 Generator.shuffleGenerator.permutation 那是 Generator.shuffle 操作到位,同时 Generator.permutation 返回一个副本。

默认情况下, Generator.permuted 返回一个副本。就地操作 Generator.permuted ,传递与第一个参数相同的数组 and 作为 out 参数。例如,

>>> rg = np.random.default_rng()
>>> x = np.arange(0, 15).reshape(3, 5)
>>> x
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> y = rg.permuted(x, axis=1, out=x)
>>> x
array([[ 1,  0,  2,  4,  3],  # random
       [ 6,  7,  8,  9,  5],
       [10, 14, 11, 13, 12]])

注意什么时候 out 则返回值为 out

>>> y is x
True

处理 axis 参数

这些方法的一个重要区别是如何处理 axis 参数。两者 Generator.shuffleGenerator.permutation 将输入视为一维序列 axis 参数确定将输入数组的哪个维度用作序列。对于二维数组, axis=0 实际上,将重新排列数组的行,并且 axis=1 将重新排列列。例如

>>> rg = np.random.default_rng()
>>> x = np.arange(0, 15).reshape(3, 5)
>>> x
array([[ 0,  1,  2,  3,  4],
       [ 5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14]])
>>> rg.permutation(x, axis=1)
array([[ 1,  3,  2,  0,  4],  # random
       [ 6,  8,  7,  5,  9],
       [11, 13, 12, 10, 14]])

请注意,列已“批量”重新排列:每列中的值没有更改。

方法 Generator.permuted 对待 axis 参数类似于 numpy.sort 治疗它。沿着给定轴的每一个片都独立于其他片进行洗牌。比较下面使用 Generator.permuted 上面的例子 Generator.permutation

>>> rg.permuted(x, axis=1)
array([[ 1,  0,  2,  4,  3],  # random
       [ 5,  7,  6,  9,  8],
       [10, 14, 12, 13, 11]])

在本例中,每行中的值(即沿 axis=1 )已经独立洗牌了。这不是对列的“批量”洗牌。

洗牌非NumPy序列

Generator.shuffle 适用于非NumPy序列。也就是说,如果给定的序列不是NumPy数组,它会将该序列洗牌到位。例如,

>>> rg = np.random.default_rng()
>>> a = ['A', 'B', 'C', 'D', 'E']
>>> rg.shuffle(a)  # shuffle the list in-place
>>> a
['B', 'D', 'A', 'E', 'C']  # random

分布

beta (a,b) [, size] )

从β分布中提取样本。

binomial (n,p) [, size] )

从二项式分布中提取样本。

chisquare (DF) [, size] )

从卡方分布中提取样本。

dirichlet α(α) [, size] )

从Dirichlet分布中提取样本。

exponential \ [scale, size] )

从指数分布中提取样本。

f \(DfNum,Dfden)[, size] )

从F分布中提取样本。

gamma [形状] [, scale, size] )

从伽马分布中提取样本。

geometric (P) [, size] )

从几何分布中提取样本。

gumbel \ [loc, scale, size] )

从Gumbel分布中提取样本。

hypergeometric \(Ngood、NBAD、NSample)[, size] )

从超几何分布中提取样本。

laplace \ [loc, scale, size] )

从拉普拉斯或双指数分布中提取具有指定位置(或平均值)和刻度(衰减)的样本。

logistic \ [loc, scale, size] )

从物流配送中抽取样本。

lognormal \ [mean, sigma, size] )

从对数正态分布中提取样本。

logseries (P) [, size] )

从对数序列分布中提取样本。

multinomial \(n,pVal\) [, size] )

从多项式分布中提取样本。

multivariate_hypergeometric \(颜色,示例)

从多元超几何分布生成变量。

multivariate_normal \(平均值),cov[, size, ...] )

从多元正态分布中随机抽取样本。

negative_binomial (n,p) [, size] )

从负二项分布中提取样本。

noncentral_chisquare \(DFNoC) [, size] )

从非中心卡方分布中提取样本。

noncentral_f \(dfnum,dfden,非[, size] )

从非中心F分布中提取样本。

normal \ [loc, scale, size] )

从正态(高斯)分布中随机抽取样本。

pareto (a) [, size] )

从指定形状的Pareto II或Lomax分布中提取样品。

poisson \ [lam, size] )

从泊松分布中提取样本。

power (a) [, size] )

提取样本 [0, 1] 从正指数a-1的幂分布。

rayleigh \ [scale, size] )

从瑞利分布中提取样本。

standard_cauchy \ [size] )

从模式为0的标准柯西分布中提取样本。

standard_exponential \ [size, dtype, method, out] )

从标准指数分布中提取样本。

standard_gamma [形状] [, size, dtype, out] )

从标准伽马分布中提取样本。

standard_normal \ [size, dtype, out] )

从标准正态分布(平均值=0,标准偏差=1)中提取样本。

standard_t (DF) [, size] )

从标准学生t分布中抽取样本 df 自由度。

triangular \(左,模式,右[, size] )

从间隔的三角形分布中提取样本 [left, right] .

uniform \ [low, high, size] )

从均匀分布中提取样品。

vonmises (Mu,Kappa) [, size] )

从von mises分布中提取样本。

wald \(平均值,比例[, size] )

从瓦尔德分布或逆高斯分布中提取样本。

weibull (a) [, size] )

从威布尔分布中提取样本。

zipf (a) [, size] )

从Zipf分布中提取样本。