了解NumPy的卷积 [英] Understanding NumPy's Convolve

查看:145
本文介绍了了解NumPy的卷积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在计算简单的移动平均线时,numpy.convolve似乎可以完成任务.

When calculating a simple moving average, numpy.convolve appears to do the job.

问题:使用np.convolve(values, weights, 'valid')时如何进行计算?

Question: How is the calculation done when you use np.convolve(values, weights, 'valid')?

当文档提到convolution product is only given for points where the signals overlap completely时,这两个信号指的是什么?

When the docs mentioned convolution product is only given for points where the signals overlap completely, what are the 2 signals referring to?

如果任何解释都可以包括示例和插图,那么它将非常有用.

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')

推荐答案

卷积是主要用于信号处理的数学运算符. Numpy只是使用此信号处理术语来定义它,因此引用了信号". numpy中的数组是一个信号.将两个信号的卷积定义为第一个信号的积分,取反,在第二个信号上扫过(卷积到"上)并在重叠矢量的每个位置乘以(与标量积).第一个信号通常称为 kernel ,尤其是当它是维基百科上的动画可以更清楚地理解它.

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 processing or neural networks, and the reversal becomes a mirroring in 2-D (NOT transpose). It can more clearly be understood using the animations on wikipedia.

根据上下文,卷积具有多个定义.有些在重叠开始时开始卷积,而另一些在重叠只是部分重叠时开始卷积.如果是numpy的有效"模式,则将重叠指定为始终完整.之所以称为有效",是因为结果中给出的每个值都是在没有数据推断的情况下完成的.

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.

例如,如果数组X的长度为2,而数组Y的长度为4,则在有效"模式下X到Y的卷积将为您提供长度为3的数组.

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 = [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

注意:如果未反转X,则该操作将称为互相关而不是卷积.

Note: If X was not reversed, the operation would be called a cross-correlation instead of a convolution.

第二步:

  [3 4]
[1 1 5 5]
= 3 * 1 + 4 * 5 = 23

第三步:

    [3 4]
[1 1 5 5]
= 3 * 5 + 4 * 5 = 35

模式有效"的卷积结果将是[7 23 35].

The result of the convolution for mode "valid" would then be [7 23 35].

如果将重叠指定为一个数据点(例如在"full"模式下的情况),则结果将为您提供长度为5的数组.第一步是:

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:

[3 4]
  [1 1 5 5]
= 3 * undefined (extrapolated as 0) + 4 * 1 = 4

以此类推.存在更多的推断模式.

And so on. More extrapolation modes exist.

这篇关于了解NumPy的卷积的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆