我有以下形状为 (3, 2, 3) 的 3d numpy 数组。
[
[[ 0 1 2]
[ 3 4 5]]
[[ 6 7 8]
[ 9 10 11]]
[[12 13 14]
[15 16 17]]
]
但是,我需要按以下顺序 reshape 为 (3, 3, 2):
[
[[ 0 3]
[ 6 9]
[12 15]]
[[ 1 4]
[ 7 10]
[13 16]]
[[ 2 5]
[ 8 11]
[14 17]]
]
我目前正在使用 Jupyter,并进行了大量的试验和错误。
谢谢您的建议!
最佳答案
要定义原始数组,您可以使用:
np.arange(18).reshape(3,2,3)
正如@Divakar 在评论中提到的,您可以使用:
np.arange(18).reshape(3,2,3).transpose(2,0,1)
得到想要的结果。
来自 np.transpose
documentation :
axes: By default, reverse the dimensions, otherwise permute the axes according to the values given.
2,0,1
是从 (3,2,3)
开始所需的排列形状为(3,3,2)
。它还会转换 (3400, 7, 100)
塑造成(100, 3400, 7)
.
另一种方法是使用 np.rollaxis
(来自@Divakar 的另一个提示):
np.rollaxis(np.arange(18).reshape(3,2,3),2)
关于python - 将 Numpy 数组从 (3, 2, 3) reshape 为 (3, 3, 2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45062921/