Reputation: 13015
I have a multidimensional array with shape (15000,1,96,96),
where each 96*96 matrix represents an image. I would like to transform its shape to (15000,96,96,1).
Can I just use a=a.reshape(15000,96,96,1)
to do it? Is that the right way?
Upvotes: 0
Views: 90
Reputation: 898
For your specific example you need to transpose the 2nd dimension with the final one. You can shuffle dimensions in a numpy array using the transpose method:
For example:
a = np.zeros((10,1,96,96))
print(a.shape)
>>> (10, 1, 96, 96)
a = a.transpose(0,2,3,1)
print(a.shape)
>>> (10, 96, 96, 1)
Upvotes: 1
Reputation: 989
To complete the comment of @OddNorg, here is an example:
Let's say you have 3 images of size (2*2):
>>>img0 = [[0,0],[0,0]]
>>>img1 = [[1,1],[1,1]]
>>>img2 = [[2,2],[2,2]]
>>>imgs = np.concatenate([[img0],[img1],[img2]])
array([[[0, 0],
[0, 0]],
[[1, 1],
[1, 1]],
[[2, 2],
[2, 2]]])
>>>imgs.shape
(3,2,2)
Now we want to have the pixels on the first and second dimensions instead of the second and the third.
if we use reshape it does work as expected :
>>>imgs_r = imgs.reshape((2,2,3))
>>>imgs_r[:,:,0]
array([[0, 0],
[1, 2]])
The first image is modified
with transpose it's working fine and images are not modified :
>>>imgs_t = imgs.tranpose((1,2,0))
>>>imgs_t[:,:,0]
array([[0, 0],
[0, 0]])
Upvotes: 1