Reputation: 1267
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.
Upvotes: 0
Views: 287
Reputation: 944
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]]])
Upvotes: 1
Reputation: 221524
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]]])
Upvotes: 2