【发布时间】:2019-08-19 03:19:29
【问题描述】:
问题
如何将 2D 矩阵重塑为 3D 矩阵,其中列“深度”移动?
我希望数组 a 看起来像数组 b
import numpy as np
a = np.array([
[1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3],
[1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3],
[1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3],
[1,1,1,1,1,1,2,2,2,2,2,2,3,3,3,3,3,3]
])
b = np.array([
[[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1],
[1,1,1,1,1,1]],
[[2,2,2,2,2,2],
[2,2,2,2,2,2],
[2,2,2,2,2,2],
[2,2,2,2,2,2]],
[[3,3,3,3,3,3],
[3,3,3,3,3,3],
[3,3,3,3,3,3],
[3,3,3,3,3,3]],
])
我想做什么
结果不是我想要达到的结果
x = np.reshape(a, (a.shape[0], -1, 3))
我还尝试了以下方法: 1) hsplit a 成 3 组 2) 尝试 dstack 这些集合但没有产生想要的结果
b = np.hsplit(a,3)
c = np.dstack([b[0],b[1],b[2]])
【问题讨论】:
标签: python numpy 3d 2d reshape