Python np.random.rand 与 np.random.random
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/47231852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
np.random.rand vs np.random.random
提问by SpaceMonkey
I find Python (and its ecosystem) to be full of strange conventions and inconsistencies and this is another example:
我发现 Python(及其生态系统)充满了奇怪的约定和不一致,这是另一个例子:
np.random.rand
np.random.rand
Create an array of the given shape and populate it with random samples from a uniform distribution over [0, 1).
创建一个给定形状的数组,并用来自 [0, 1) 上均匀分布的随机样本填充它。
np.random.random
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.
在半开区间 [0.0, 1.0) 中返回随机浮点数。结果来自规定区间内的“连续均匀”分布。
??? What exactly is the difference there?
???究竟有什么区别呢?
采纳答案by Warren Weckesser
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 answerfor more aliases.)
首先请注意,这numpy.random.random
实际上是numpy.random.random_sample
. 我将在下面使用后者。(有关更多别名,请参阅此问题和答案。)
Both functions generate samples from the uniform distributionon [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.
这两个函数都从[0, 1) 上的均匀分布中生成样本。唯一的区别在于如何处理参数。使用numpy.random.rand
,输出数组的每个维度的长度是一个单独的参数。使用numpy.random.random_sample
,形状参数是单个元组。
For example, to create an array of samples with shape (3, 5), you can write
例如,要创建一个形状为 (3, 5) 的样本数组,您可以编写
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:
从 1.17 版开始,NumPy 有一个新的random API。从 [0, 1) 上的均匀分布生成样本的推荐方法是:
>>> rng = np.random.default_rng() # Create a default Generator.
>>> rng.random(size=12) # 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 isa uniform()
method that allows you to specify the lower and upper bounds of the distribution. E.g.
新Generator
类没有rand()
orrandom_sample()
方法。有是一个uniform()
允许您指定分布的上限和下限的方法。例如
>>> 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.
numpy.random
命名空间中的旧函数将继续工作,但它们被视为“冻结”,没有持续的开发。如果您正在编写新代码,并且您不必支持 1.17 之前的 numpy 版本,则建议您使用新的 random API。