Python 理解 NumPy 的 Convolve
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20036663/
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
Understanding NumPy's Convolve
提问by Nyxynyx
When calculating a simple moving average, numpy.convolveappears to do the job.
在计算简单的移动平均线时,numpy.convolve似乎可以完成这项工作。
Question:How is the calculation done when you use np.convolve(values, weights, 'valid')?
问题:使用时如何计算np.convolve(values, weights, 'valid')?
When the docs mentioned convolution product is only given for points where the signals overlap completely, what are the 2 signals referring to?
当文档提到时convolution product is only given for points where the signals overlap completely,这两个信号指的是什么?
If any explanations can include examples and illustrations, it will be extremely useful.
如果任何解释可以包括示例和插图,那将非常有用。
window = 10
weights = np.repeat(1.0, window)/window
smas = np.convolve(values, weights, 'valid')
回答by Soravux
Convolution is a mathematical operator primarily used in signal processing. Numpy simply uses this signal processing nomenclature to define it, hence the "signal" references. An array in numpy is a signal. The convolution of two signals is defined as the integral of the first signal, reversed, sweeping over ("convolved onto") the second signal and multiplied (with the scalar product) at each position of overlapping vectors. The first signal is often called the kernel, especially when it is a 2-D matrix in image processingor neural networks, and the reversalbecomes a mirroring in 2-D(NOT transpose). It can more clearly be understood using the animations on wikipedia.
卷积是一种主要用于信号处理的数学运算符。Numpy 只是使用此信号处理术语来定义它,因此称为“信号”引用。numpy 中的数组是一个信号。两个信号的卷积被定义为第一个信号的积分,反转,扫过(“卷积到”)第二个信号并在重叠向量的每个位置相乘(与标量积)。第一个信号通常称为核,特别是当它是图像处理或神经网络中的二维矩阵时,反转变成二维中的镜像(非转置)。使用wikipedia 上的动画可以更清楚地理解它。
Convolutions have multiple definitions depending on the context. Some start the convolution when the overlap begins while others start when the overlap is only partial. In the case of numpy's "valid" mode, the overlap is specified to be always complete. It is called "valid" since every value given in the result is done without data extrapolation.
根据上下文,卷积有多种定义。有些在重叠开始时开始卷积,而另一些在重叠只是部分时开始。在 numpy 的“有效”模式的情况下,重叠被指定为总是完整的。它被称为“有效”,因为结果中给出的每个值都是在没有数据外推的情况下完成的。
For instance, if your array X have a length of 2 and your array Y have a length of 4, the convolution of X onto Y in "valid" mode will give you an array of length 3.
例如,如果数组 X 的长度为 2,数组 Y 的长度为 4,则在“有效”模式下 X 到 Y 的卷积将为您提供长度为 3 的数组。
First step, for X = [4 3]and Y = [1 1 5 5]:
第一步,对于X = [4 3]和Y = [1 1 5 5]:
[3 4] (X is reversed from [4 3] to [3 4], see note)
[1 1 5 5]
= 3 * 1 + 4 * 1 = 7
Note: If X was not reversed, the operation would be called a cross-correlationinstead of a convolution.
注意:如果 X 没有反转,则该操作将称为互相关而不是卷积。
Second Step:
第二步:
[3 4]
[1 1 5 5]
= 3 * 1 + 4 * 5 = 23
Third step:
第三步:
[3 4]
[1 1 5 5]
= 3 * 5 + 4 * 5 = 35
The result of the convolution for mode "valid" would then be [7 23 35].
模式“有效”的卷积结果将是 [7 23 35]。
If the overlap is be specified as one single data point (as the case in mode "full"), the result would have given you an array of length 5. The first step being:
如果重叠被指定为一个单个数据点(如模式“完整”中的情况),结果将为您提供一个长度为 5 的数组。第一步是:
[3 4]
[1 1 5 5]
= 3 * undefined (extrapolated as 0) + 4 * 1 = 4
And so on. More extrapolation modes exist.
等等。存在更多的外推模式。

