Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading