2

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

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

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]] 
]

and so on.

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.

6
  • 1
    Is this what you want arr.reshape(9,2,3,3)? Commented Sep 27, 2019 at 12:40
  • @yatu, no I want 3D arrays instead vectors Commented Sep 27, 2019 at 12:44
  • Do you want a 4D array or a list of 3D arrays? Commented Sep 27, 2019 at 12:45
  • @Valentino, 4D nympy array Commented Sep 27, 2019 at 12:47
  • It looks to me like your desired output is 5D with shape (3,3,3,2,3) ? Commented Sep 27, 2019 at 12:48

1 Answer 1

2

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]]],
# 
# 
# ...
Sign up to request clarification or add additional context in comments.

8 Comments

Would you explain these numbers?
They're the numbers you gave in your question. -1 means "infer this value from the remaining numbers"
Specifically, reading the linked documentation for reshape, it is "The new shape should be compatible with the original shape. If an integer, then the result will be a 1-D array of that length. One shape dimension can be -1. In this case, the value is inferred from the length of the array and remaining dimensions."
I need some another order. Zero element must be ```[ [[0, 205, 25], [210, 150, 30]], [[0, 255, 0], [255, 40, 0]], [[0, 0, 30], [0, 40, 0]] ]
@NilsWerner, such a strange thing. I have another outpit [[[ 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]] ...]
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.