这个 Generator 提供对广泛分布的访问,并作为 RandomState . 两者的主要区别在于 Generator 依赖于一个额外的位生成器来管理状态并生成随机位,然后将这些随机位从有用的分布转换为随机值。所使用的默认位生成器 Generator 是 PCG64 . 位生成器可以通过将实例化的位生成器传递给 Generator .
Generator
RandomState
PCG64
numpy.random.
default_rng
用默认的位生成器(PCG64)构造一个新的生成器。
初始化的种子 BitGenerator . 如果没有,那么新的,不可预测的熵将从操作系统中提取。如果 int 或 array_like[ints] 如果通过了,那么它将被传递给 SeedSequence 导出初始值 BitGenerator 州。你也可以通过 SeedSequence 实例。此外,当通过 BitGenerator ,它将由 Generator . 如果通过了 Generator ,则会原封不动地返回。
BitGenerator
int
array_like[ints]
SeedSequence
初始化的生成器对象。
笔记
如果 seed 不是一个 BitGenerator 或A Generator ,一个新的 BitGenerator 已实例化。此函数不管理默认全局实例。
seed
实例
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]])
比特发生器的容器。
Generator 显示从各种概率分布中生成随机数的许多方法。除了特定于分布的参数外,每个方法都采用关键字参数 size 默认为 None .如果 size 是 None ,然后生成并返回单个值。如果 size 是一个整数,然后返回一个包含生成值的一维数组。如果 size 是一个元组,然后填充并返回具有该形状的数组。
None
函数 numpy.random.default_rng 将实例化 Generator 以numpy的默认值 BitGenerator .
numpy.random.default_rng
无兼容性保证
Generator 不提供版本兼容性保证。尤其是,随着更好的算法的发展,比特流可能会改变。
比特发生器用作核心发生器。
参见
推荐建造商 Generator .
Python stdlib模块 random 包含伪随机数生成器,其中包含许多与中提供的方法类似的方法 Generator . 它使用Mersenne Twister,这个位生成器可以使用 MT19937 . Generator 除了 NumPy 的意识之外,它的优点是它提供了更多的概率分布供选择。
random
MT19937
>>> from numpy.random import Generator, PCG64 >>> rg = Generator(PCG64()) >>> rg.standard_normal() -0.203 # random
bit_generator
获取生成器使用的位生成器实例
integers (低) [, high, size, dtype, endpoint] )
integers
从返回随机整数 low (包括) high (独占),或者如果endpoint=True, low (包括) high (包括在内)。
random \ [size, dtype, out] )
返回半开区间内的随机浮点数[0.0,1.0]。
choice (a) [, size, replace, p, axis, shuffle] )
choice
从给定的一维数组生成随机样本
bytes (长度)
bytes
返回随机字节。
随机排列序列的方法有
shuffle (x) [, axis] )
shuffle
通过改变序列的内容来修改序列。
permutation (x) [, axis] )
permutation
随机排列序列,或返回排列范围。
permuted (x) [, axis, out] )
permuted
随机排列 x 沿轴线 axis .
下表总结了这些方法的行为。
方法
复制/就位
轴线处理
随机播放
就位
好像1d
置换
复制
置换的
任意(使用“out”表示“in-place”)
轴独立
以下小节提供了有关这些差异的更多详细信息。
两者的主要区别 Generator.shuffle 和 Generator.permutation 那是 Generator.shuffle 操作到位,同时 Generator.permutation 返回一个副本。
Generator.shuffle
Generator.permutation
默认情况下, Generator.permuted 返回一个副本。就地操作 Generator.permuted ,传递与第一个参数相同的数组 and 作为 out 参数。例如,
Generator.permuted
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.shuffle 和 Generator.permutation 将输入视为一维序列 axis 参数确定将输入数组的哪个维度用作序列。对于二维数组, axis=0 实际上,将重新排列数组的行,并且 axis=1 将重新排列列。例如
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 :
numpy.sort
>>> rg.permuted(x, axis=1) array([[ 1, 0, 2, 4, 3], # random [ 5, 7, 6, 9, 8], [10, 14, 12, 13, 11]])
在本例中,每行中的值(即沿 axis=1 )已经独立洗牌了。这不是对列的“批量”洗牌。
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] )
beta
从β分布中提取样本。
binomial (n,p) [, size] )
binomial
从二项式分布中提取样本。
chisquare (DF) [, size] )
chisquare
从卡方分布中提取样本。
dirichlet α(α) [, size] )
dirichlet
从Dirichlet分布中提取样本。
exponential \ [scale, size] )
exponential
从指数分布中提取样本。
f \(DfNum,Dfden)[, size] )
f
从F分布中提取样本。
gamma [形状] [, scale, size] )
gamma
从伽马分布中提取样本。
geometric (P) [, size] )
geometric
从几何分布中提取样本。
gumbel \ [loc, scale, size] )
gumbel
从Gumbel分布中提取样本。
hypergeometric \(Ngood、NBAD、NSample)[, size] )
hypergeometric
从超几何分布中提取样本。
laplace \ [loc, scale, size] )
laplace
从拉普拉斯或双指数分布中提取具有指定位置(或平均值)和刻度(衰减)的样本。
logistic \ [loc, scale, size] )
logistic
从物流配送中抽取样本。
lognormal \ [mean, sigma, size] )
lognormal
从对数正态分布中提取样本。
logseries (P) [, size] )
logseries
从对数序列分布中提取样本。
multinomial \(n,pVal\) [, size] )
multinomial
从多项式分布中提取样本。
multivariate_hypergeometric \(颜色,示例)
multivariate_hypergeometric
从多元超几何分布生成变量。
multivariate_normal \(平均值),cov[, size, ...] )
multivariate_normal
从多元正态分布中随机抽取样本。
negative_binomial (n,p) [, size] )
negative_binomial
从负二项分布中提取样本。
noncentral_chisquare \(DFNoC) [, size] )
noncentral_chisquare
从非中心卡方分布中提取样本。
noncentral_f \(dfnum,dfden,非[, size] )
noncentral_f
从非中心F分布中提取样本。
normal \ [loc, scale, size] )
normal
从正态(高斯)分布中随机抽取样本。
pareto (a) [, size] )
pareto
从指定形状的Pareto II或Lomax分布中提取样品。
poisson \ [lam, size] )
poisson
从泊松分布中提取样本。
power (a) [, size] )
power
提取样本 [0, 1] 从正指数a-1的幂分布。
rayleigh \ [scale, size] )
rayleigh
从瑞利分布中提取样本。
standard_cauchy \ [size] )
standard_cauchy
从模式为0的标准柯西分布中提取样本。
standard_exponential \ [size, dtype, method, out] )
standard_exponential
从标准指数分布中提取样本。
standard_gamma [形状] [, size, dtype, out] )
standard_gamma
从标准伽马分布中提取样本。
standard_normal \ [size, dtype, out] )
standard_normal
从标准正态分布(平均值=0,标准偏差=1)中提取样本。
standard_t (DF) [, size] )
standard_t
从标准学生t分布中抽取样本 df 自由度。
triangular \(左,模式,右[, size] )
triangular
从间隔的三角形分布中提取样本 [left, right] .
[left, right]
uniform \ [low, high, size] )
uniform
从均匀分布中提取样品。
vonmises (Mu,Kappa) [, size] )
vonmises
从von mises分布中提取样本。
wald \(平均值,比例[, size] )
wald
从瓦尔德分布或逆高斯分布中提取样本。
weibull (a) [, size] )
weibull
从威布尔分布中提取样本。
zipf (a) [, size] )
zipf
从Zipf分布中提取样本。
numpy.random