Home > OS >  How to solve error when reshaping DataFrame to LSTM
How to solve error when reshaping DataFrame to LSTM

Time:12-06

I have CSV data with shapes (6042, 6) like this and call it using DataFrame:

enter image description here

I want to use it for input on LSTM. From what I read, LSTM requires data in the form of a 3d array [samples, time steps, features]. Because the DataFrame doesn't have a reshape attribute, I changed it to a NumPy array and tried to reshape it, but get the error:

TypeError: only integer scalar arrays can be converted to a scalar index

I tried using the following code:

train_data = train_data.to_numpy()
train_data = train_data.reshape(1, train_data[0], train_data[1])

Is my step wrong?

CodePudding user response:

Based on reshape documentation, the format for calling the routine is:

numpy.reshape(a, newshape, order='C')

Therefore, you should do...

import numpy    
numpy.reshape(train_data, (1, train_data[0], train_data[1]))

...assuming the shape above is what you want.

CodePudding user response:

You code is returning an error message because, when you write train_data[0], you are getting the first line of the train_data 2-dimentional numpy array:

>>> df = pd.DataFrame([[.4,.6,.3], [.7,.8,.9]])
>>> df
     0    1    2
0  0.4  0.6  0.3
1  0.7  0.8  0.9
>>> df = df.to_numpy()
>>> df[0]
array([0.4, 0.6, 0.3])

What you actually want is to use the dataframe's shape. Try this:

>>> df = df.to_numpy()
>>> df = df.reshape(1, df.shape[0], df.shape[1])
>>> df
array([[[0.4, 0.6, 0.3],
        [0.7, 0.8, 0.9]]])
>>> df.shape
(1, 2, 3)
  • Related