np.random.rand 与 np.random.random [英] np.random.rand vs np.random.random
问题描述
我发现 Python(及其生态系统)充满了奇怪的约定和不一致,这是另一个例子:
np.random.rand
<块引用>创建一个给定形状的数组,并用来自 [0, 1 上的均匀分布的随机样本填充它.
np.random.random
<块引用>返回半开区间 [0.0, 1.0) 中的随机浮点数.结果来自指定区间内的连续均匀"分布.
???到底有什么区别?
首先要注意 numpy.random.random
实际上是 numpy.random.random_sample
.我将在下面使用后者.(有关更多别名,请参阅此问答.)
这两个函数都从 [0, 1) 上的均匀分布生成样本.唯一的区别在于如何处理参数.使用 numpy.random.rand
,输出数组的每个维度的长度是一个单独的参数.对于 numpy.random.random_sample
,shape 参数是一个元组.
例如,要创建一个形状为 (3, 5) 的样本数组,您可以编写
sample = np.random.rand(3, 5)
或
sample = np.random.random_sample((3, 5))
(真的,就是这样.)
更新
从 1.17 版开始,NumPy 有一个新的random API.从 [0, 1) 上的均匀分布生成样本的推荐方法是:
<预><代码>>>>rng = np.random.default_rng() # 创建一个默认的生成器.>>>rng.random(size=10) # 生成 10 个样本.数组([0.00416913, 0.31533329, 0.19057857, 0.48732511, 0.40638395,0.32165646, 0.02597142, 0.19788567, 0.08142055, 0.15755424])新的 Generator
类没有有 rand()
或 random_sample()
方法.有一个 uniform()
方法,允许您指定分布的下限和上限.例如
numpy.random
命名空间中的旧函数将继续工作,但它们被视为冻结",没有持续的开发.如果您正在编写新代码,并且您不必支持 1.17 之前的 numpy 版本,则建议您使用新的 random API.
I find Python (and its ecosystem) to be full of strange conventions and inconsistencies and this is another example:
np.random.rand
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
np.random.random
Return random floats in the half-open interval [0.0, 1.0). Results are from the "continuous uniform" distribution over the stated interval.
??? What exactly is the difference there?
First note that numpy.random.random
is actually an alias for numpy.random.random_sample
. I'll use the latter in the following. (See this question and answer for more aliases.)
Both functions generate samples from the uniform distribution on [0, 1). The only difference is in how the arguments are handled. With numpy.random.rand
, the length of each dimension of the output array is a separate argument. With numpy.random.random_sample
, the shape argument is a single tuple.
For example, to create an array of samples with shape (3, 5), you can write
sample = np.random.rand(3, 5)
or
sample = np.random.random_sample((3, 5))
(Really, that's it.)
Update
As of version 1.17, NumPy has a new random API. The recommended method for generating samples from the uniform distribution on [0, 1) is:
>>> rng = np.random.default_rng() # Create a default Generator.
>>> rng.random(size=10) # Generate 10 samples.
array([0.00416913, 0.31533329, 0.19057857, 0.48732511, 0.40638395,
0.32165646, 0.02597142, 0.19788567, 0.08142055, 0.15755424])
The new Generator
class does not have the rand()
or random_sample()
methods. There is a uniform()
method that allows you to specify the lower and upper bounds of the distribution. E.g.
>>> rng.uniform(1, 2, size=10)
array([1.75573298, 1.79862591, 1.53700962, 1.29183769, 1.16439681,
1.64413869, 1.7675135 , 1.02121057, 1.37345967, 1.73589452])
The old functions in the numpy.random
namespace will continue to work, but they are considered "frozen", with no ongoing development. If you are writing new code, and you don't have to support pre-1.17 versions of numpy, it is recommended that you use the new random API.
这篇关于np.random.rand 与 np.random.random的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!