2

I want to use keras to apply a neural network to my time-series data. TO improve the model I want to have 50 time states of input per output. The final input should have 951 samples with 50 time points of 10 features (951, 50, 10)

Therefore, I have to reshape my data. I do that doing a for loop, but is awfully slow. Is there a way to improve the code and making it faster?

Example:

import numpy as np
X = np.ones((1000,10))

for i in range(50, int(X.shape[0]) + 1):
     if i == 50:
        z = 0
        X2 = np.array(X[z:i, :]).reshape((1, 50, X.shape[1]))
     else:
        X2 = np.concatenate([X2, np.array(X[z:i, :]).reshape((1, 50, X.shape[1]))])
     z = z + 1
freddy888
  • 956
  • 3
  • 18
  • 39

1 Answers1

2

We can leverage np.lib.stride_tricks.as_strided based scikit-image's view_as_windows to get sliding windows. More info on use of as_strided based view_as_windows.

from skimage.util.shape import view_as_windows

X2 = view_as_windows(X,(50,10))[:,0]

It's simply a view into the input and hence virtually free on runtime -

In [17]: np.shares_memory(X,view_as_windows(X,(50,10))[:,0])
Out[17]: True

In [18]: %timeit view_as_windows(X,(50,10))[:,0]
10000 loops, best of 3: 32.8 µs per loop
Divakar
  • 218,885
  • 19
  • 262
  • 358