如何从Python numpy 3D数组转换为2D再转换为1D再转换为2D(保留3D数组的原始第2维和第3维)

时间:2019-05-03 18:20:49

标签: python arrays numpy reshape

我有一个3D阵列,一个:

`print(a.shape)
In [1]:(4, 4571, 8893)
b = a.reshape(a.shape[2]*a.shape[1],a.shape[0]) # Here I've also tried changing the shape of with (a.shape[2]*a.shape[1],a.shape[0])
print(b.shape)
In [2]:(40649903, 4)
c=some_function(b) # returns c which has same shape as b.shape[0]
print(c.shape)
In [2]: (40649903,)
d = c.reshape(a.shape[1],a.shape[2]) # same shape as a.shape[1:]
print(d.shape)
In [3]:(4571, 8893)

`

现在,当我看着d时,我会得到一个像这样的形状:

plt.imshow(d)

enter image description here

但是它必须类似于下图所示(请忽略颜色,黄色区域的形状必须类似于海军蓝色区域):

plt.imshow(a[0])

enter image description here

也许这与重塑轴有关,但我无法弄清楚我使用错误的轴进行重塑的位置。我已经对此进行了一些思考,并阅读了numpy文档,但是文档和在线示例(SO问题)似乎没有针对我的特定问题的清晰示例。我所缺少的任何方向都会有所帮助。

1 个答案:

答案 0 :(得分:0)

以下是可能存在相同问题的其他人的答案:

a = np.arange(24).reshape(4,3,2)
print(a); print(a.shape)

b = a.reshape(a.shape[0],a.shape[1]*a.shape[2]).T; 
print(b); print(b.shape) # X

c = a[0].flatten() # Y
print(c); print(c.shape)

d = c.reshape(a[1].shape); 
print(d); print(d.shape) # same as print(a[0].shape)

感谢您的建议@hpaulj