Issue
I'm wonder how can I undo my transpose operation. Let me be more specific in example:
a = np.random.rand(25,32,11)
b = a.transpose(2,0,1)
c = b.transpose(??) ### Here I should set (1,0,2)
# c == a
Which exactly values should I set in last transpose to make c == a ? In numpy there is not such method as "transpose_undo" I guess. As an solution we can rely on actual shape of array, but we can have 25x25x25 array in the future...
Solution
Using transpose, just follow the order. Your first permutation mapped dimensions as:
0th transformed is 2nd original
1st transformed is 0th original
2nd transformed is 1st original
-------------------
0th original is 1st transformed
1st original is 2nd transformed
2nd original is 0th transformed
a = np.random.rand(25,32,11)
b = a.transpose(2,0,1)
np.all(a == b.transpose(1, 2, 0))
yields true
EDIT:
if you want to automate inverse permutation you can use np.argsort
axes = [2, 0, 1]
b = a.transpose(*axes)
np.all(a == b.transpose(*np.argsort(axes)) # yields true
Answered By - Dominik Ficek
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.