For higher dimensional arrays, transpose will accept a tuple of axis numbers to permute the axes (for extra mind bending):
对于更高维度的数组,转置将接受轴编号的元组来置换轴(用于额外的心灵弯曲):
In [115]: arr = np.arange(16).reshape((2, 2, 4))
In [116]: arr
Out[116]:
array([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7]],
[[ 8, 9, 10, 11],
[12, 13, 14, 15]]])
In [117]: arr.transpose((1, 0, 2))
Out[117]:
array([[[ 0, 1, 2, 3],
[ 8, 9, 10, 11]],
[[ 4, 5, 6, 7],
[12, 13, 14, 15]]])
I am trying to understand how above output is generated for transpose((1, 0, 2). I am not able to understand what is meant is permute the axes? Request to explain in layman terms and how above output is generated.
我试图理解如何为转置生成输出((1,0,2)。我无法理解是什么意思是对轴进行置换?请求用外行术语解释以及如何生成输出。
Thanks
3 个解决方案
#1
1
The default transpose is to reverse the axes, so an A x B
matrix becomes B x A
. For 3D, the default would be to transpose A x B x C
to C x B x A
.
默认转置是反转轴,因此A x B矩阵变为B x A.对于3D,默认设置是将A x B x C转置为C x B x A.
In your example, transpose(1, 0, 2)
, it will transpose A x B x C
to B x A x C
. That's because the default 3D transpose is (2, 1, 0)
, but you have (1, 0, 2)
which simply swaps the first two axes.
在你的例子中,转置(1,0,2),它会将A x B x C转换为B x A x C.这是因为默认的3D转置是(2,1,0),但你有(1,0) ,2)简单地交换前两个轴。
When you are experimenting, it may be more clear if you use an example array of shape 2 x 3 x 4
or some other combination which has no duplicates.
在进行实验时,如果使用形状为2 x 3 x 4的示例数组或其他没有重复的组合,则可能会更清楚。
#2
0
Consider an array and its transpose:
考虑一个数组及其转置:
arr = np.arange(24).reshape((2, 3, 4))
arrt = arr.transpose((1, 0, 2))
By default transpose
just reverses the order of dimensions. The particular transpose command above swaps the first two dimensions but leaves the last dimension untouched. Let's verify in a few examples:
默认情况下,转置只会反转维度的顺序。上面的特定转置命令交换前两个维度但保持最后一个维度不变。让我们在几个例子中验证:
print(arr.shape)
# (2, 3, 4)
print(arrt.shape)
# (3, 2, 4)
# the last dimension is the same in both arrays
print(arr[0, 0, :])
# [0 1 2 3]
print(arrt[0, 0, :])
# [0 1 2 3]
print(arr[:, 0, 0]) # what was before the first dimension
# [ 0 12]
print(arrt[0, :, 0]) # is now the second dimension
# [ 0 12]
# the first two dimensions are swapped - the submatrix is transposed
print(arr[:, :, 0])
# [[ 0 4 8]
# [12 16 20]]
print(arrt[:, :, 0])
# [[ 0 12]
# [ 4 16]
# [ 8 20]]
#3
0
Transposing a matrix is essentially switching the order of your dimensions, e.g.: arr.transpose()
is in this case equal to arr.transpose((2,1,0))
.
转置矩阵本质上是切换维度的顺序,例如:arr.transpose()在这种情况下等于arr.transpose((2,1,0))。
On the other hand, if you wish to select the order of your dimensions by hand, you will preserve the original order (i.e. don't change anything) by transposing with arr.transpose((0,1,2))
.
另一方面,如果您希望手动选择尺寸的顺序,您将通过使用arr.transpose((0,1,2))进行转置来保留原始顺序(即不要更改任何内容)。
In your example you leave the last dimension (2) of your array, e.g. [0,1,2,3]
, unchanged. However, you switch the first two (0 and 1), thus elements at arr[0,0,0:4]
will still be there, but the contents of arr[1,0,0:4]
now appear at arr[0,1,0:4]
after transposing:
在您的示例中,您将保留数组的最后一个维度(2),例如[0,1,2,3],不变。但是,你切换前两个(0和1),因此arr [0,0,0:4]的元素仍然存在,但arr [1,0,0:4]的内容现在出现在arr [转置后0,1,0:4]:
In[]: t_arr = arr.transpose((1,0,2))
In[]: arr[0,0,0:4] == t_arr[0,0,0:4]
Out[]:
array([ True, True, True, True], dtype=bool)
In[]: arr[0,1,0:4] == t_arr[1,0,0:4] # indices #0,1,0-3 and #1,0,0-3 respectively
Out[]:
array([ True, True, True, True], dtype=bool)
This is also what you expect when transposing a multidimensional matrix. The axes are swapped in a way, so that the elements are located at almost the same indices as before, just with the swapped order, i.e.:
这也是您在转置多维矩阵时所期望的。轴以某种方式交换,因此元素的位置几乎与以前相同,只是交换顺序,即:
arr[x,y,z] == arr.transpose()[z,y,x] # True
arr[x,y,z] == arr.transpose((1,0,2))[y,x,z] # True (your case)
#1
1
The default transpose is to reverse the axes, so an A x B
matrix becomes B x A
. For 3D, the default would be to transpose A x B x C
to C x B x A
.
默认转置是反转轴,因此A x B矩阵变为B x A.对于3D,默认设置是将A x B x C转置为C x B x A.
In your example, transpose(1, 0, 2)
, it will transpose A x B x C
to B x A x C
. That's because the default 3D transpose is (2, 1, 0)
, but you have (1, 0, 2)
which simply swaps the first two axes.
在你的例子中,转置(1,0,2),它会将A x B x C转换为B x A x C.这是因为默认的3D转置是(2,1,0),但你有(1,0) ,2)简单地交换前两个轴。
When you are experimenting, it may be more clear if you use an example array of shape 2 x 3 x 4
or some other combination which has no duplicates.
在进行实验时,如果使用形状为2 x 3 x 4的示例数组或其他没有重复的组合,则可能会更清楚。
#2
0
Consider an array and its transpose:
考虑一个数组及其转置:
arr = np.arange(24).reshape((2, 3, 4))
arrt = arr.transpose((1, 0, 2))
By default transpose
just reverses the order of dimensions. The particular transpose command above swaps the first two dimensions but leaves the last dimension untouched. Let's verify in a few examples:
默认情况下,转置只会反转维度的顺序。上面的特定转置命令交换前两个维度但保持最后一个维度不变。让我们在几个例子中验证:
print(arr.shape)
# (2, 3, 4)
print(arrt.shape)
# (3, 2, 4)
# the last dimension is the same in both arrays
print(arr[0, 0, :])
# [0 1 2 3]
print(arrt[0, 0, :])
# [0 1 2 3]
print(arr[:, 0, 0]) # what was before the first dimension
# [ 0 12]
print(arrt[0, :, 0]) # is now the second dimension
# [ 0 12]
# the first two dimensions are swapped - the submatrix is transposed
print(arr[:, :, 0])
# [[ 0 4 8]
# [12 16 20]]
print(arrt[:, :, 0])
# [[ 0 12]
# [ 4 16]
# [ 8 20]]
#3
0
Transposing a matrix is essentially switching the order of your dimensions, e.g.: arr.transpose()
is in this case equal to arr.transpose((2,1,0))
.
转置矩阵本质上是切换维度的顺序,例如:arr.transpose()在这种情况下等于arr.transpose((2,1,0))。
On the other hand, if you wish to select the order of your dimensions by hand, you will preserve the original order (i.e. don't change anything) by transposing with arr.transpose((0,1,2))
.
另一方面,如果您希望手动选择尺寸的顺序,您将通过使用arr.transpose((0,1,2))进行转置来保留原始顺序(即不要更改任何内容)。
In your example you leave the last dimension (2) of your array, e.g. [0,1,2,3]
, unchanged. However, you switch the first two (0 and 1), thus elements at arr[0,0,0:4]
will still be there, but the contents of arr[1,0,0:4]
now appear at arr[0,1,0:4]
after transposing:
在您的示例中,您将保留数组的最后一个维度(2),例如[0,1,2,3],不变。但是,你切换前两个(0和1),因此arr [0,0,0:4]的元素仍然存在,但arr [1,0,0:4]的内容现在出现在arr [转置后0,1,0:4]:
In[]: t_arr = arr.transpose((1,0,2))
In[]: arr[0,0,0:4] == t_arr[0,0,0:4]
Out[]:
array([ True, True, True, True], dtype=bool)
In[]: arr[0,1,0:4] == t_arr[1,0,0:4] # indices #0,1,0-3 and #1,0,0-3 respectively
Out[]:
array([ True, True, True, True], dtype=bool)
This is also what you expect when transposing a multidimensional matrix. The axes are swapped in a way, so that the elements are located at almost the same indices as before, just with the swapped order, i.e.:
这也是您在转置多维矩阵时所期望的。轴以某种方式交换,因此元素的位置几乎与以前相同,只是交换顺序,即:
arr[x,y,z] == arr.transpose()[z,y,x] # True
arr[x,y,z] == arr.transpose((1,0,2))[y,x,z] # True (your case)