简体   繁体   中英

How to convert list of numpy arrays to numpy array

I want to make a sliding window for LSTM. For this I have:

x_train=[[]]
y_train = []

for i in range(10, len(train_data)):
  x_train.append(train_data[i-10:i])
  y_train.append(train_data[i,0])

The shape of train_data is (2730, 2), so I suppose x_train will be (2721, 10, 2).
After the loop x_train and y_train are lists of numpy arrays.
In tutorials it's enough to apply np.asarray or np.array to change it to numpy array. In my case it changes shape to (2721, ) and that's not really what I expected.
Probably, it's better to use numpy arrays and not lists.
But I wonder why in tutorials the way I do works and it doesn't for me. Maybe there is a small error or something in the code?

PS Sorry, I found an error by myself. It was x_train=[[]] , but it should be x_train=[] .

Thank you for your comments. Actually in that situation it was a good solution to concatenate numpy arrays. Here I made an error creating a wrong type of list, so the fix is rather obvious, just to replace x_train=[[]] by x_train=[] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM