Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

python - Understanding NumPy's Convolve

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

Question: How is the calculation done when you use 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?

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')
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

First step, for X = [4 3] and 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-correlation instead of a convolution.

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].

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...