Python Numpy: reverse transpose

Viewed 127

suppose I have an array of shape (2,2,3): x=np.array([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]), and I have transposed this array to y along axes (2,0,1): y=np.transpose(x,(2,0,1)).

My questions is, how to transpose y back to x? I know I can use np.transpose(y,(1,2,0)) to change back to x, but I'd like to have a neat way of finding that correct axes (1,2,0).

Thanks for any help!

1 Answers

You can use

np.argsort((2, 0, 1))

It gives:

array([1, 2, 0])

It will work with any permutation of axes.

Related