[英]Convert a list of numpy arrays to a 5D numpy array
I am having a database of 7000 objects (list_of_objects), each one of these files contains a numpy array with size of 10x5x50x50x3
. 我有7000个对象(list_of_objects)的数据库,这些文件中的每个文件都包含一个numpy数组,大小为
10x5x50x50x3
。 I would like to create a 5d numpy array that will contain 7000*10x5x50x50x3
. 我想创建一个5d numpy数组,其中将包含
7000*10x5x50x50x3
。 I tried to do so using two for-loops. 我尝试使用两个for循环来这样做。 My sample code:
我的示例代码:
fnl_lst = []
for object in list_of_objects:
my_array = read_array(object) # size 10x5x50x50x3
for ind in my_array:
fnl_lst.append(ind)
fnl_lst= np.asarray( fnl_lst) # print(fnl_lst) -> (70000,)
That code result in the end in a nested numpy array which contains 70000 arrays each of them has a size of 5x50x50x3
. 该代码最终导致嵌套的numpy数组,其中包含70000个数组,每个数组的大小为
5x50x50x3
。 However, I would like instead to build a 5d array with size 70000x5x50x50x3
. 但是,我想建立一个大小为
70000x5x50x50x3
的5d数组。 How can I do that instead? 我该怎么做呢?
fnl_lst = np.stack([ind for ind in read_array(obj) for obj in list_of_objects])
or, just append to the existing code: 或者,只需追加到现有代码:
fnl_lst = np.stack(fnl_lst)
UPD: by hpaulj's comment, if my_array
is indeed 10x5x50x50x3, this might be enough: UPD:根据hpaulj的评论,如果
my_array
的确为10x5x50x50x3,则可能就足够了:
fnl_lst = np.stack([read_array(obj) for obj in list_of_objects])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.