0

I have an array that I would like to reshape for the purposes of training an LSTM in Python. This is what my array looks like:

[[0,0,0,0,1]
[1,0,0,0,0]
[0,0,0,1,0]
[0,1,0,0,0]]

In this above example, there are 5 features. The other 4 features at that point are set to 0, and one feature is 1. I would like to reshape this using the (sample,timestep,feature) to create a 3D array.

I am using numpy, and naturally, the .reshape() function would work great.

4
  • 1
    Its already in a shape of (timestep, feature) isn't it? If you want to add a dimension here just do x[None,...]. If you want to stack a few of these do np.array(x_list). Commented Jul 27, 2020 at 0:06
  • @sachinruk Yes, correct, it is in the timestamp,feature shape. You said I should add a dimension by doing x[None,...], I don't really understand that syntax. Can you clarify? Commented Jul 27, 2020 at 0:09
  • 1
    Putting None adds a dimension to make it (1, 4, 5) in your case. 1 is the sample size in this case. You would want a lot more samples for a LSTM. Commented Jul 27, 2020 at 0:15
  • It does, however, I wanted to inquire more about in which direction I should reshape the array... like whether it should be size (1,4,5), (4,1,5), or (4,5,1). Commented Jul 27, 2020 at 0:22

1 Answer 1

1

It is easy to do with numpy using .reshape method:

A = np.array([[0,0,0,0,1], [1,0,0,0,0], [0,0,0,1,0], [0,1,0,0,0]])
A = A.reshape(2, 2, 5)
print(A.shape)

So new shape is (2, 2, 5). For your data you can just add a dummy dimension for a time step:

A = np.expan_dims(A, 1)
Sign up to request clarification or add additional context in comments.

4 Comments

Hi, I would not like to create two samples at every single timestep. I want to maintain the current format, but make it three dimensional.
Thank you for updating your response. Upon doing expand_dims on my (2,5) array, I now get a (2,1,5) array. Should it be (2,1,5) or (2,5,1)?
It is all up to you, how you reshape the array. If you use np.expand_dims(A, -1), you will get (2, 5, 1). But for lstm, the input shape is (batch_size, time_step, embedding_size). Your 0s and 1s are definitely embedding rather than time steps.
That clarifies things. The expand_dims definitely helped. +1 Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.