1、不加参数的情况下,transpose()的作用就是整个矩阵完全置换
import numpy as np
A = np.arange(24).reshape(2,3,4)
T = A.transpose()
print(T.shape)
print('______________')
print(A)
print('______________')
print(T)
print('______________')
print(A[1,2,3])
print(T[3,2,1])
output:
(4, 3, 2)
______________
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
______________
[[[ 0 12]
[ 4 16]
[ 8 20]]
[[ 1 13]
[ 5 17]
[ 9 21]]
[[ 2 14]
[ 6 18]
[10 22]]
[[ 3 15]
[ 7 19]
[11 23]]]
______________
23
23
2、加参数时,必须根据矩阵的维度来设置参数
import numpy as np
#对于该矩阵可以认为有三个维度,即0,1,2
A = np.arange(24).reshape((2,3,4))
print(A)
#如果不改变原矩阵,那么正常的参数顺序是:(0,1,2)
T = A.transpose(0,1,2)
print(T)
print('_____________________')
#如果想要置换第1和第2个维度,则参数顺序为:(1, 0 ,2)
T = A.transpose(1,0,2)
print(A[0,1,2])
print(T[1,0,2])
print('_____________________')
#如果要置换第1和第3个维度,则参数的顺序为:(2,1,0)
T = A.transpose(2,1,0)
print(A[0,1,2])
print(T[2,1,0])
output:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
_____________________
6
6
_____________________
6
6