我有一个3d数组如下:
import numpy an np
arr3D = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]],
[[13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24]]])
或
[[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
[[13 14 15]
[16 17 18]
[19 20 21]
[22 23 24]]]
我想重新排序我的行,如下所示:
[[[10 11 12]
[ 7 8 9]
[ 4 5 6]
[ 1 2 3]]
[[22 23 24]
[19 20 21]
[16 17 18]
[13 14 15]]]
我想这样做的一种方法是使用.argsort()
,但我很困惑,如果有可能做到这一点,还是我需要使用其他策略?
您可以使用::-1
在您喜欢的每个轴上反转。
>>> arr3D[:, ::-1, :] # <- you want this, reverse on axis=1
array([[[10, 11, 12],
[ 7, 8, 9],
[ 4, 5, 6],
[ 1, 2, 3]],
[[22, 23, 24],
[19, 20, 21],
[16, 17, 18],
[13, 14, 15]]])
# Another example, reverse on axis=2 or axis=-1
>>> arr3D[...,::-1]
array([[[ 3, 2, 1],
[ 6, 5, 4],
[ 9, 8, 7],
[12, 11, 10]],
[[15, 14, 13],
[18, 17, 16],
[21, 20, 19],
[24, 23, 22]]])