Search code examples
pythonarraysperformancenumpytranspose

Best way to transpose nested arrays within array than a for loop


I have an numpy array of shape (43200, 28, 28), which contains 43200 images of size 28x28. I'd like to transpose each of these images but can't find a better way to do it than with a loop such as

for i in range(arr.shape[0]):
  arr[i] = arr[i].T

Is there a more efficient way ?


Solution

  • You can preserve the 0 axis, but transpose the latter two

    arr.transpose(0, 2, 1)