0

I'm struggling to make LSTM work. I've found a question on Stack Overflow:

Neural Network LSTM input shape from dataframe

That seemed to help me but actually I ran into another problem: cannnot reshape data, but I do all steps like in 'instruction'.

I have 48 rows × 22 columns dataset where first column is date. Label column is separated from this dataset. So I have 21 prediction features.

but when I do

# Extract your training data
X_train_init = np.asarray(DF.padded_input_vectors)
print(X_train_init.shape)
# Use hstack to and reshape to make the inputs a 3d vector
X_train = np.hstack(X_train_init).reshape(len(DF),max_sequence_length,21)
y_train = np.hstack(np.asarray(train_target)).reshape(len(DF),1)

I get:

(48,)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-14-ea37bd9226df> in <module>
      3 print(X_train_init.shape)
      4 # Use hstack to and reshape to make the inputs a 3d vector
----> 5 X_train = np.hstack(X_train_init).reshape(len(DF),max_sequence_length,21)
      6 y_train = np.hstack(np.asarray(train_target)).reshape(len(DF),1)

ValueError: cannot reshape array of size 48 into shape (48,48,21)
halfer
  • 19,824
  • 17
  • 99
  • 186

2 Answers2

0

A shape of (48,) is a single array of size 48. For a shape of (48, 48, 21), you would need 48,384 elements, so you can't resize 48 elements to that shape. Maybe you need to add elements in some way? In any case, that's why the reshape is failing.

Edit: Check your import, that seems to be the issue.

iHowell
  • 2,263
  • 1
  • 25
  • 49
0

The LSTM model requires a 3D input in the form of [samples, time steps, features]

Thus the X_train should have data in the 3D format.

The train data should be such that it can be correctly reshaped into this 3D format.

This can be understood with a simple example. eg. we have 10 students in total and for each of them, there are 5-week records of online learning interactions found for 4 different subjects( features/columns).

This means the data frame consists of 10*5 = 50 rows and 4 columns (subjects). Now, this data can be reshaped easily into 3D format as

X_train_shaped= np.reshape(X_train.values,(10,5,4))

However, the code in question cannot reshape a single vector of 48 into (48,48,21) as the data is not accurate for 3D representation.