[SOLVED] How to cut a Numpy 2d array and store as a 3d array?

Issue

suppose I have the following Numpy 2d array (3*9)

[[-2 -2 -2 -2 -2 -2 -2 -2 -2]
 [-2 -2 -2 -2 -2 -2 -2 -2 -2]
 [-2 -2 71 -1 -1 -1 71 -1 52]]

I would like to equally divide this whole array by 3 and get the target array like this, which means I should get a 3d array of shape(3 by 3 by3):

[[-2 -2 -2] [-2 -2 -2] [-2 -2 -2]
 [-2 -2 -2] [-2 -2 -2] [-2 -2 -2]
 [-2 -2 71] [-1 -1 -1] [71 -1 52]]

Anyone can explain how to do this?

Solution

If you already know the previous shape and target shape, you can simply use reshape

arr = [[-2, -2, -2, -2, -2, -2, -2, -2, -2],
[-2, -2, -2, -2, -2, -2, -2, -2, -2,],
[-2, -2, 71, -1, -1, -1, 71, -1, 52]]
arr = np.array(arr)
arr.reshape(3,3,3)

Answered By – Frederick Zhang

Answer Checked By – Timothy Miller (BugsFixing Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *