1

I have a numpy array from which I have to generate moving windows with the defined window and stride length. Below is the general idea explained with sample data and functions. I am able to achieve what I want in the below code. But I have millions of records and I want this part of the code to be efficient. Can someone suggest an efficient way to write this code?

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)

I also found pairwise and grouped functions could be used to select the windows of a particular length. If so pls help me.

MRP
  • 11
  • 3

0 Answers0