0

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?

2 Answers 2

2

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

1 Comment

Note that you could let numpy do the calculation for you: arr.reshape(3,-1,3)
1

you can use reshape method like the following

import numpy as np
array = np.array([[-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]])

newarr = array.reshape(3,3,3)
print(newarr)

Comments

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.