移动窗口在numpy与动态步幅和窗口大小

时间:2016-12-29 04:57:48

标签: arrays python-3.x pandas numpy

我有一个numpy数组,我必须使用定义的窗口和步长生成移动窗口。以下是使用示例数据和函数解释的一般概念。我能够在下面的代码中实现我想要的。但我有数百万条记录,我希望这部分代码高效。有人可以提出一种有效的方法来编写这段代码吗?

def funcA(val):  #sample dummy functions
    return val+2
def funcB(val):
    return val+3

strideLength = 2 # would increment array index by two in next iteration
windowLength = 4 # number of elements needed in a particular window
a = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15])
for index in range(0,len(a)-(windowLength-1),strideLength):
    x_g = np.empty(windowLength)
    y_c = np.empty(windowLength)
    b = a[index:index+windowLength]
    for i in range(0,len(b)):
        x_g[i] = funcA(b[i]) # this function used to select a part of pandas dataframe so x_g[i] would contain a pandas dataframe
        y_c[i] = funcB(b[i]) 
    print("x_g : ",x_g) # do further computation with these array
    print("y_c : ",y_c)

我还发现成对和分组函数可用于选择特定长度的窗口。如果有,请帮助我。

0 个答案:

没有答案