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?
CodePudding user response:
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)
CodePudding user response:
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)