numpy.correlate: full 和 valid 之间的关系

numpy.correlate: Relation between full and valid

我在两组数据上使用 numpy.correlate 两个 20000 值都很大。

文档表明:

mode : {‘full’, ‘valid’, ‘same’}, optional ‘full’:

By default, mode is ‘full’. This returns the convolution at each point of overlap, with an output shape of (N+M-1,). At the end-points of the convolution, the signals do not overlap completely, and boundary effects may be seen.

‘same’: Mode same returns output of length max(M, N). Boundary effects are still visible.

‘valid’: Mode valid returns output of length max(M, N) - min(M, N) + 1. The convolution product is only given for points where the signals overlap completely. Values outside the signal boundary have no effect

按照这些解释,我希望我的集合 "valid" 选项会给我与使用 "full" 选项返回的值编号 20000 获得的值相同的值,因为仅适用于这个值集合叠加。但是这两个值是不同的。

谁能解释一下它是如何工作的?

请记住,Python 索引从零开始而不是 1。您需要索引 19999 而不是 20000:

x = np.random.randn(20000)
y = np.random.randn(20000)

np.correlate(x, y, 'valid')[0]
# -29.778322045152521

np.correlate(x, y, 'full')[19999]
# -29.778322045152521