简体   繁体   English

如何将 numpy arrays 列表转换为 numpy 数组

[英]How to convert list of numpy arrays to numpy array

I want to make a sliding window for LSTM.我想为 LSTM 制作一个滑动的 window。 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). train_data的形状是 (2730, 2),所以我想x_train将是 (2721, 10, 2)。
After the loop x_train and y_train are lists of numpy arrays.循环后x_trainy_train是 numpy arrays 的列表
In tutorials it's enough to apply np.asarray or np.array to change it to numpy array.在教程中,应用np.asarraynp.array将其更改为 numpy 数组就足够了。 In my case it changes shape to (2721, ) and that's not really what I expected.在我的情况下,它的形状变为 (2721, ),这并不是我所期望的。
Probably, it's better to use numpy arrays and not lists.可能,最好使用 numpy arrays 而不是列表。
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. PS对不起,我自己发现了一个错误。 It was x_train=[[]] , but it should be x_train=[] .它是x_train=[[]] ,但应该是x_train=[]

Thank you for your comments.谢谢您的意见。 Actually in that situation it was a good solution to concatenate numpy arrays.实际上在这种情况下,连接 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=[] .在这里,我在创建错误类型的列表时出错,因此修复相当明显,只需将x_train=[[]]替换为x_train=[]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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