我想要做的是生成一个 numpy 数组,它是给定某个窗口的另一个 numpy 数组的累积和。
例如,给定一个数组,[1,2,3,4,5,6,7,8,9,10,11,12]
假设我想要一个窗口为 3 的累积和。我想要的输出是[1,3,6,9,12,15,18,21,24,27,30,33]
. 我有一个相对较大的 numpy 数组,想用 400 的窗口做一个累积和。
我想要做的是生成一个 numpy 数组,它是给定某个窗口的另一个 numpy 数组的累积和。
例如,给定一个数组,[1,2,3,4,5,6,7,8,9,10,11,12]
假设我想要一个窗口为 3 的累积和。我想要的输出是[1,3,6,9,12,15,18,21,24,27,30,33]
. 我有一个相对较大的 numpy 数组,想用 400 的窗口做一个累积和。
这可能是一个更简单的答案,基于减去移位的 cumsums。
>>> a = np.array([1,2,3,4,5,6,7,8,9,10,11,12])
>>> b = a.cumsum()
>>> b[3:] = b[3:] - b[:-3]
>>> b
array([ 1, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33])
你应该使用 numpy,除非你真的不关心速度(尽管我更喜欢它)。因此,您可以使用卷积或stride_tricks
基于方法(这些方法并不明显,但可以很好地解决这些问题)。
例如给定一个这样的函数(你也可以找到更多更好的版本):
def embed(array, dim, lag=1):
"""Create an embedding of array given a resulting dimension and lag.
The array will be raveled before embedding.
"""
array = np.asarray(array)
array = array.ravel()
new = np.lib.stride_tricks.as_strided(array,
(len(array)-dim*lag+lag, dim),
(array.strides[0], array.strides[0]*lag))
return new
你可以做:
embedded = embed(array, 400)
result = embedded.sum(1)
哪个是内存效率高的(嵌入或任何你称之为的,只会创建一个视图)并且速度很快。当然,另一种方法是使用卷积:
np.convolve(array, np.ones(400), mode='valid')
我不知道您是否也想要非完整窗口,这与使用mode='full'
(默认)进行卷积相同。对于另一种方法,必须以其他方式处理。
In [42]: lis=[1,2,3,4,5,6,7,8,9,10,11,12]
In [43]: w=3 #window size
In [44]: [sum(lis[i-(w-1):i+1]) if i>(w-1) else sum(lis[:i+1]) for i in range(len(lis))]
Out[44]: [1, 3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33]
In [45]: w=4
In [46]: [sum(lis[i-(w-1):i+1]) if i>(w-1) else sum(lis[:i+1]) for i in range(len(lis))]
Out[46]: [1, 3, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42]
对于 python 2.4 或更低版本,更改三元运算符:
(falseValue, trueValue)[condition]
代替trueValue if condition else falseValue
[(sum(lis[:i+1]),sum(lis[i-(w-1):i+1]))[i>(w-1)] for i in range(len(lis))]
seberg 的答案比我的更好,更通用,但请注意,您需要对样本进行零填充以获得所需的结果。
import numpy as np
from numpy.lib.stride_tricks import as_strided as ast
samples = 100
window = 3
padding = np.zeros(window - 1)
# zero-pad your samples
a = np.concatenate([padding,np.arange(1,samples + 1)])
newshape = (len(a) - window,window)
newstrides = a.strides * 2
# this gets you a sliding window of size 3, with a step of 1
strided = ast(a,shape = newshape,strides = newstrides)
# get your moving sum
strided.sum(1)