【问题标题】:Convert a list of numpy arrays to a 5D numpy array将 numpy 数组列表转换为 5D numpy 数组
【发布时间】:2019-08-30 23:49:45
【问题描述】:

我有一个包含 7000 个对象 (list_of_objects) 的数据库,每个文件都包含一个大小为 10x5x50x50x3 的 numpy 数组。我想创建一个包含7000*10x5x50x50x3 的 5d numpy 数组。我尝试使用两个 for 循环来做到这一点。我的示例代码:

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,)

该代码最终生成一个嵌套的 numpy 数组,其中包含 70000 个数组,每个数组的大小为 5x50x50x3。但是,我想构建一个大小为70000x5x50x50x3 的 5d 数组。我该怎么做呢?

【问题讨论】:

  • 您确定所有ind 的形状都相同吗? np.vstack([read_array(o) for o in list_of_objects]) 产生了什么?
  • @hpaulj 我会反复核对。也许这是我的问题,即 ind 由于某种原因没有相同的大小。
  • 什么是fnl_lst.dtype?如果object,那么是的,形状有一些变化。
  • True one 文件大小不同,无法监控。现在它可以正常工作了。

标签: python arrays list numpy


【解决方案1】:
fnl_lst = np.stack([ind for ind in read_array(obj) for obj in list_of_objects])

或者,只是附加到现有代码:

fnl_lst = np.stack(fnl_lst)

UPD:根据 hpaulj 的评论,如果 my_array 确实是 10x5x50x50x3,这可能就足够了:

fnl_lst = np.stack([read_array(obj) for obj in list_of_objects])

【讨论】:

    猜你喜欢
    • 2017-03-08
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 2020-08-02
    • 1970-01-01
    • 1970-01-01
    • 2015-01-07
    相关资源
    最近更新 更多