将3D numpy数组拆分为较小的3D数组 [英] Split a 3D numpy array into smaller 3D arrays

查看:61
本文介绍了将3D numpy数组拆分为较小的3D数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个3D np.array

I have a 3D np.array

arr = np.array([ 
                [ [0, 205, 25], [210, 150, 30], [0, 0, 0], [1, 2, 3], [4, 5, 6], [7, 8, 9] ],
                [ [0, 255, 0], [255, 40, 0], [0, 0, 200], [7, 8, 9], [10, 11, 12], [120, 51, 58] ],
                [ [0, 0, 30], [0, 40, 0], [200, 100, 20], [12, 13, 14], [15, 16, 17], [13, 78, 84], ],
                [ [0, 205, 25], [210, 150, 30], [0, 0, 0], [1, 2, 3], [4, 5, 6], [7, 8, 9] ],
                [ [0, 255, 0], [255, 40, 0], [0, 0, 200], [7, 8, 9], [10, 11, 12], [120, 51, 58] ],
                [ [0, 0, 30], [0, 40, 0], [200, 100, 20], [12, 13, 14], [15, 16, 17], [13, 78, 84], ],
                [ [0, 205, 25], [210, 150, 30], [0, 0, 0], [1, 2, 3], [4, 5, 6], [7, 8, 9] ],
                [ [0, 255, 0], [255, 40, 0], [0, 0, 200], [7, 8, 9], [10, 11, 12], [120, 51, 58] ],
                [ [0, 0, 30], [0, 40, 0], [200, 100, 20], [12, 13, 14], [15, 16, 17], [13, 78, 84], ],
              ])

我需要将其拆分为3x2x3 3D阵列

And I need to split it to 3x2x3 3D arrays

[ [0, 205, 25], [210, 150, 30],    [0, 0, 0], [1, 2, 3],             [4, 5, 6], [7, 8, 9] ],
[ [0, 255, 0],  [255, 40, 0],      [0, 0, 200], [7, 8, 9],           [10, 11, 12], [120, 51, 58] ],
[ [0, 0, 30],   [0, 40, 0],        [200, 100, 20], [12, 13, 14],     [15, 16, 17], [13, 78, 84], ],

[ [0, 205, 25], [210, 150, 30],    [0, 0, 0], [1, 2, 3],             [4, 5, 6], [7, 8, 9] ],
[ [0, 255, 0],  [255, 40, 0],      [0, 0, 200], [7, 8, 9],           [10, 11, 12], [120, 51, 58] ],
[ [0, 0, 30],   [0, 40, 0],        [200, 100, 20], [12, 13, 14],     [15, 16, 17], [13, 78, 84], ],

[ [0, 205, 25], [210, 150, 30],    [0, 0, 0], [1, 2, 3],             [4, 5, 6], [7, 8, 9] ],
[ [0, 255, 0],  [255, 40, 0],      [0, 0, 200], [7, 8, 9],           [10, 11, 12], [120, 51, 58] ],
[ [0, 0, 30],   [0, 40, 0],        [200, 100, 20], [12, 13, 14],     [15, 16, 17], [13, 78, 84], ],

使用我用空格选择的这些3D块获得4D阵列.零元素必须为

to get a 4D array with these 3D blocks I've selected by spaces. Zero element must be

[ 
    [[0, 205, 25], [210, 150, 30]],
    [[0, 255, 0], [255, 40, 0]],
    [[0, 0, 30], [0, 40, 0]] 
]

以此类推.

我已经阅读了这个问题,但仍然没有并不要做这件事(为什么我们需要重塑,转置和重塑,以及 transpose()中的神奇数字).我可以尝试编写自己的函数,但我想知道如何以本机的方式进行操作.

I've read this question but still don't undersatand how to do this (Why we need to reshape, transpose and reshape again and what a magical numbers in transpose()). I could try to write my own function but I want to know how to do it native way.

推荐答案

您可以重塑形状并转置它

You can reshape and transpose it

arr.reshape(3, 3, 3, 2, 3).transpose(2, 0, 1, 3, 4)
# array([[[[[  0, 205,  25],
#           [210, 150,  30]],
# 
#          [[  0, 255,   0],
#           [255,  40,   0]],
# 
#          [[  0,   0,  30],
#           [  0,  40,   0]]],
# 
# 
#         [[[  0, 205,  25],
#           [210, 150,  30]],
# 
#          [[  0, 255,   0],
#           [255,  40,   0]],
# 
#          [[  0,   0,  30],
#           [  0,  40,   0]]],
# 
# 
# ...

这篇关于将3D numpy数组拆分为较小的3D数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆