我在这个课间学习Numpy :-D,今天我遇到了转置。我可以很好地理解2D矩阵的转置,但很难理解3D矩阵(数组)的转置。有人能在下面的片段中解释一下a4是如何受到.transpose()的影响的吗?当然,我可以在这里找到一个模式,但我想知道转置背后的一般原理,以便我能够将其应用于任何维度的矩阵。任何帮助都是非常感谢的。
In [84]: a4 = np.random.randint(12,size=(3,2,3))
a4
array([[[ 2, 10, 8],
[ 1, 4, 9]],
[[ 9, 10, 2],
[10, 5, 9]],
[[ 0, 5, 2],
[ 6, 8, 2]]])
In [85]: a4.T
array([[[ 2, 9, 0],
[ 1, 10, 6]],
[[10, 10, 5],
[ 4, 5, 8]],
[[ 8, 2, 2],
[ 9, 9, 2]]])发布于 2020-04-09 10:51:00
帮助我思考转置的是意识到形状数组在转置时会像镜像操作一样被翻转。如下所示:
a2 = np.random.randint(12,size=(3,2))
print('{} <=> {}'.format(a2.shape, a2.T.shape))
a3 = np.random.randint(12,size=(3,2,4))
print('{} <=> {}'.format(a3.shape, a3.T.shape))
a4 = np.random.randint(12,size=(3,2,4,5))
print('{} <=> {}'.format(a4.shape, a4.T.shape)) 结果:
(3, 2) <=> (2, 3)
(3, 2, 4) <=> (4, 2, 3)
(3, 2, 4, 5) <=> (5, 4, 2, 3)https://stackoverflow.com/questions/61112988
复制相似问题