沿行将2D数组重塑为3D数组

时间:2019-04-30 20:41:10

标签: python arrays numpy

我有3n行和m列的2D numpy数组。如何将其重塑为3维numpy数组,该数组由n行,m列和3个切片组成,沿第3维。

2D to 3D transformation rowise

1 个答案:

答案 0 :(得分:0)

好的,这是一种解决方案。我相信还有其他方法可以做到。

import numpy as np
#Generate 24 element 1D array and reshape to 6 rows and 4 columns
a_Array = arange(24).reshape(6, 4)
#reshape to distribute data along axis 2 then axis 1 and then axis 0 in order, 
#this is what numpy reshape does by default
a_Array.reshape(2,3,4)
#Swap axes 1 and 2
a_Array.reshape(2,3,4).swapaxes(1,2)
#Transpose to arrange data with data distributed along axis 1
a_Array.reshape(2,3,4).swapaxes(1,2).T

enter image description here

Transpose Visualisation

链接1通常帮助理解多维数组。 链接2用于生成附有图片的视觉效果。