0

I have trained a NN in Keras with LSTM, so I have been using 3D tensors. Now I want to predict with a dataset and I have to insert a 3D tensor in my NN.

(In my case I used features = 2 and lookback = 2, so input elements in LSTM are (batch_size, lookback, features))

So, imagine this example:

a = np.array([[1, 2],
              [3, 4]])

I need to do a_2 = np.reshape(1, 2, 2) to be able to insert it in the LSTM.

But if I have a bigger test dataset, like for instance:

b = np.array([[1, 2],
              [3, 4],
              [5, 6],
              [7, 8],
              [9, 10]])

I would need to convert it to a 3D array of this type:

b_2 = np.array([[[1, 2],
                 [3, 4]],

                [[3, 4],
                 [5, 6]],

                [[5, 6],
                 [7, 8]],

                [[7, 8],
                 [9, 10]]])

so in this case I have predictions for each lookback with the new point. I guess this could be done with a complicated solution using many for loops nested, but I wonder if there is more pythonic way. Thx.

1
  • Yes, thank you, they are both useful. Commented Mar 9, 2020 at 10:31

2 Answers 2

2

You are looking for sliding windows and there's skimage's view_as_windows for that -

In [46]: from skimage.util.shape import view_as_windows

In [44]: features = 2; lookback = 2

In [45]: view_as_windows(b,(lookback, features))[:,0]
Out[45]: 
array([[[ 1,  2],
        [ 3,  4]],

       [[ 3,  4],
        [ 5,  6]],

       [[ 5,  6],
        [ 7,  8]],

       [[ 7,  8],
        [ 9, 10]]])
Sign up to request clarification or add additional context in comments.

Comments

1

Use numpy.stack() as follows.

>>> np.stack((b[:-1], b[1:]), axis=1)
array([[[ 1,  2],
        [ 3,  4]],

       [[ 3,  4],
        [ 5,  6]],

       [[ 5,  6],
        [ 7,  8]],

       [[ 7,  8],
        [ 9, 10]]])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.