randn 和 normal 之间的 Python 区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21738383/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 23:30:41  来源:igfitidea点击:

Python difference between randn and normal

pythonnumpy

提问by Medulla Oblongata

I'm using the randnand normalfunctions from Python's numpy.randommodule. The functions are pretty similar from what I've read in the http://docs.scipy.orgmanual (they both concern the Gaussian distribution), but are there any subtler differences that I should be aware of? If so, in what situations would I be better off using a specific function?

我正在使用Python模块中的randnnormal函数numpy.random。这些函数与我在http://docs.scipy.org手册中读到的非常相似(它们都涉及高斯分布),但是我应该注意哪些细微的差异?如果是这样,在什么情况下我最好使用特定功能?

采纳答案by M4rtini

randnseems to give a distribution from some standardized normal distribution (mean 0 and variance 1). normaltakes more parameters for more control. So randseems to simply be a convenience function

randn似乎给出了一些标准化正态分布(均值为 0 和方差为 1)的分布。 normal需要更多参数以获得更多控制。所以rand似乎只是一个方便的功能

回答by Mike Williamson

I'm a statistician who sometimes codes, not vice-versa, so this is something I can answer with some accuracy.

我是一名统计学家,有时会编码,反之亦然,所以我可以准确地回答这个问题。

Looking at the docs that you linked in your question, I'll highlight some of the key differences:

查看您在问题中链接的文档,我将重点介绍一些主要差异:

normal:

普通的:

numpy.random.normal(loc=0.0, scale=1.0, size=None)
# Draw random samples from a normal (Gaussian) distribution.

# Parameters :  
# loc : float -- Mean (“centre”) of the distribution.
# scale : float -- Standard deviation (spread or “width”) of the distribution.
# size : tuple of ints -- Output shape. If the given shape is, e.g., (m, n, k), then m * n * k samples are drawn.

So in this case, you're generating a GENERICnormal distribution (more details on what that means later).

因此,在这种情况下,您将生成一个GENERIC正态分布(稍后将详细介绍这意味着什么)。

randn:

兰登:

numpy.random.randn(d0, d1, ..., dn)
# Return a sample (or samples) from the “standard normal” distribution.

# Parameters :  
# d0, d1, ..., dn : int, optional -- The dimensions of the returned array, should be all positive. If no argument is given a single Python float is returned.
# Returns : 
# Z : ndarray or float -- A (d0, d1, ..., dn)-shaped array of floating-point samples from the standard normal distribution, or a single such float if no parameters were supplied.

In this case, you're generating a SPECIFICnormal distribution, the standard distribution.

在这种情况下,您将生成一个特定的正态分布,即标准分布。



Now some of the math, which is really needed to get at the heart of your question:

现在有一些数学,真正需要了解您的问题的核心:

A normal distribution is a distribution where the values are more likelyto occur near the mean value. There are a bunch of cases of this in nature. E.g., the average high temperature in Dallas in June is, let's say, 95 F. It might reach 100, or even 105 average in one year, but it more typically will be near 95 or 97. Similarly, it might reach as low as 80, but 85 or 90 is more likely.

正态分布是其中值更可能出现在平均值附近的分布。自然界中有很多这样的案例。例如,达拉斯 6 月份的平均高温是,比方说,95 F。它可能在一年内达到 100,甚至 105 的平均值,但更典型的是接近 95 或 97。同样,它可能会达到低至80,但 85 或 90 的可能性更大。

So, it is fundamentally different from, say, a uniform distribution (rolling an honest 6-sided die).

因此,它与均匀分布(掷一个诚实的 6 面骰子)有着根本的不同。



A standardnormal distribution is just a normal distribution where the average value is 0, and the variance (the mathematical term for the variation) is 1.

标准正态分布是只是一个普通的分布,其中平均值为0和方差(用于变化的数学术语)为1。

So,

所以,

numpy.random.normal(size= (10, 10))

is the exact same thing as writing

和写作完全一样

numpy.random.randn(10, 10)

because the defaultvalues (loc= 0, scale= 1) for numpy.random.normalare in fact the standarddistribution.

因为默认值 (loc= 0, scale= 1)numpy.random.normal实际上是标准分布。

To make matters more confusing, as the numpy random documentationstates:

更令人困惑的是,正如numpy random 文档所述:

sigma * np.random.randn(...) + mu

is the same as

是相同的

np.random.normal(loc= mu, scale= sigma, ...)


*Final note: I used the term variance to mathematically describe variation. Some folks say standard deviation. Variance simply equals the square of standard deviation. Since the variance = 1 for the standard distribution, in this case of the standard distribution, variance == standard deviation.

*最后一点:我使用术语方差来数学描述变化。有人说标准差。方差简单地等于标准偏差的平方。由于标准分布的方差 = 1,因此在标准分布的这种情况下,variance == standard deviation.

回答by Sumanth Lazarus

Following up to @Mike Williamson's explanation about variance, standard deviation, I was caught trying to workout the example provided in the Numpy documentation for randnThe example provided there:

按照@Mike Williamson 关于方差、标准偏差的解释,我被发现试图锻炼 randn 的Numpy 文档中提供的示例 那里提供的示例:

Create (2x4) Array/Matrix whose distribution has Mean = 3, Variance = 6.25

创建 (2x4) 数组/矩阵,其分布的均值 = 3,方差 = 6.25

The point to note here is that Normal Distribution follows notation N(Mean, Variance), whereas to implement using .randn()you would require to multiply the standard deviationor sigmaand add the Meanor muto the Standard Normal Output of the Numpy method(s).

这里要注意的一点是正态分布遵循符号 N(Mean, Variance),而要实现使用,.randn()您需要乘以标准差sigma并将平均值mu添加到 Numpy 方法的标准正态输出.

Note:

笔记:

sqrt(Variance) = Standard Deviation or sigma

sqrt(Variance) = 标准偏差或西格玛

sqrt(6.25) = 2.5

平方(6.25)= 2.5

Hence:

因此:

sigma * numpy.random.randn(2, 4) + mean

sigma * numpy.random.randn(2, 4) + mean