1

I have a NumPy multi dimensional array of shape (1,76,76,255). I want to reshape it to another multidimensional array of shape (1,255,76,76). It’s still a 4D array, but I need to change the data indices I guess.

Is there an easy way without using loops?

3
  • 1
    Have you looked at np.transpose? Commented Mar 16, 2022 at 2:25
  • 1
    @hpaulj np.transpose will give you arr.shape[::-1] not the one OP wants Commented Mar 16, 2022 at 2:28
  • 2
    @tbhaxor, have you looked at the np.transpose docs? Commented Mar 16, 2022 at 6:55

1 Answer 1

1

The function you are looking for is np.moveaxis() which lets you move a source axis to its destination.

>>> arr = np.random.random((1,76,76,255))
>>> 
>>> arr.shape
(1, 76, 76, 255)
>>> arr2 = np.moveaxis(arr, 3, 1)
>>> arr2.shape
(1, 255, 76, 76)
>>> 

Please note that these axes are 0-indexed

Sign up to request clarification or add additional context in comments.

2 Comments

transpose can do anything moveaxs can. arr.tranpose(0,3,1,2)
I didn't know that. Thanks for mentioning @hpaulj

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.