numpy.关联vs numpy文档这里有矛盾吗?为什么结果列表颠倒了?

2024-03-29 07:33:19 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用numpy的关联函数得到以下结果:

In [153]: np.correlate([1],np.arange(100))
Out[153]:
array([99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83,
       82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66,
       65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49,
       48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32,
       31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15,
       14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0])

In [154]:

这一结果似乎与90年代的情况相矛盾。numpybook页面:

enter image description here

根据上面的公式,我希望数组0..99增加,但是结果是数组99..0减少。你知道吗

有人能解释一下这是怎么回事吗?你知道吗

为什么实现与规范相矛盾?你知道吗

为什么颠倒清单是有意义的?你知道吗


Tags: 函数innumpy规范np情况页面数组
1条回答
网友
1楼 · 发布于 2024-03-29 07:33:19

像你期待的那样。你链接到的那本书很旧(2006年),所以它看起来像是numpy.correlate自写完后就已经改变了(它实际上是在^{}中改变的)。从docs for ^{}

old_behavior : bool

If True, uses the old behavior from Numeric, (correlate(a,v) == correlate(v,a), and the conjugate is not taken for complex arrays). If False, uses the conventional signal processing definition.

In [2]: np.correlate([1],np.arange(100))
Out[2]: 
array([99, 98, 97, 96, 95, 94, 93, 92, 91, 90, 89, 88, 87, 86, 85, 84, 83,
   82, 81, 80, 79, 78, 77, 76, 75, 74, 73, 72, 71, 70, 69, 68, 67, 66,
   65, 64, 63, 62, 61, 60, 59, 58, 57, 56, 55, 54, 53, 52, 51, 50, 49,
   48, 47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32,
   31, 30, 29, 28, 27, 26, 25, 24, 23, 22, 21, 20, 19, 18, 17, 16, 15,
   14, 13, 12, 11, 10,  9,  8,  7,  6,  5,  4,  3,  2,  1,  0])

In [3]: np.correlate([1],np.arange(100),old_behavior=True)
Out[3]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
   17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
   34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
   51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
   68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
   85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

In [4]: np.correlate(np.arange(100),[1])
Out[4]: 
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
   17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
   34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
   51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
   68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
   85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])

编辑

进一步检查,我认为差异是由于旧定义中的这一行:

K=len(x)-1 and M=len(y)-1, and we assume K ≥ M (without loss of generality because we can interchange the roles of x and y without effect).

所以,我相信对于你的例子,在旧的定义中,它是y=[1]x=np.arange(100),因为len(x)必须大于len(y)。新定义没有这样做,而是"input arrays are never swapped",因此x=[1]y=np.arange(100)。因此,差异。你知道吗

相关问题 更多 >