问题
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')
回答1:
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.
来源:https://stackoverflow.com/questions/20036663/understanding-numpys-convolve