Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Numpy: reverse transpose

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!

like image 820
Sam-gege Avatar asked Sep 03 '25 15:09

Sam-gege


1 Answers

You can use

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

It gives:

array([1, 2, 0])

It will work with any permutation of axes.

like image 54
bb1 Avatar answered Sep 05 '25 05:09

bb1