如何在Python中使用滑动重叠窗口消除For循环?

时间:2019-04-23 21:06:43

标签: python pandas loops numpy

由于For循环在Python中运行缓慢,因此我想避免使用它们。我该如何向量化或使用方法来加速涉及滑动重叠窗口的代码?

data = np.mgrid[0:6, 0:2].reshape(3,-1).T
window_length=3
value = np.zeros(len(data) - window_length + 1)


for j in range(0,len(data)-window_length):
    window_mean = np.mean(data[j:j + window_length,:], axis=1)
    for k in range(j, j + window_length):
        #just some operation  
        value[j] = np.matmul(np.transpose(data[k,:]), data[k, :] + window_mean)

0 个答案:

没有答案