I was curious if anyone had any advice on how to reshape data for a recurrent neural network. What I've been doing is array.reshape(len(X_train), # of points in time, # of features)

And then in the array, the X axis represents the features, and for each item, there are entries corresponding to the number of points in time (and a column with id so I know what the original id is). So if the time series has 12 points, each sample will have 12 entries in a row in the array.

Am I reshaping correctly, and if not, how can I reshape correctly?

Edit The array initially looks like this: enter image description here

This is a pandas array which I convert to numpy by using values.astype(float). The article, and timestep column are just for reference. I am using the command array.reshape(-1, 12, 17) The -1 is the length of the array, 12 is the timesteps (or number of entries in y axis for each article), and the 17 is the number of features at each time step. I am then feeding that data to a recurrent neural network.


1 Answer 1


An LSTM takes in a sequence of items. In Tensorflow, this takes the form of a tensor with the shape $[ batch, sequence, dim ]$, where:

  • $batch$ is the size of the batch
  • $sequence$ is the sequence length
  • $dim$ is the size oft he embedding dimension.

For example, I could have a tensor in the shape $[ 64, 100, 25 ]$, and $64$ would be the batch size, $100$ would be the sequence length, and $25$ would be the size of the embedding dimension.

To be more specific, on the sequence dimension here each element is an item in the sequence.

Given the specifics of your data, each time step would be a single item in this sequence, and the features would be the embedding space, giving you a tensor in the form $[ timesteps, features ]$ for each input sample to your model, which would then be stacked as $[ batch_size, timesteps, features ]$ by e.g. Tensorflow in your data pipeline.

  • $\begingroup$ In regards to the data, I have data for 17 different features of data at 12 different time points for lots of different articles (about 70 thousand). What I'm curious about is how to setup the array and then reshape it. The way I currently have it looks like this: $\endgroup$ May 26, 2022 at 19:37
  • $\begingroup$ I can't figure out how to format it: but I will upload to original post. $\endgroup$ May 26, 2022 at 19:40
  • $\begingroup$ Updated answer based on your additional information. $\endgroup$ May 27, 2022 at 18:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .