首页 > 解决方案 > 3D 数组 - 将先前的 2D 数组值作为第 3 维添加到新的 3D 数组

问题描述

我正在尝试通过循环现有的 2D 数组从现有的 2D 数组中创建一个新的 3D 数组,并将 2D 数组的所有先前行添加到新 3D 数组的 Z 轴(深度)。

我尝试将现有数组重塑为 3D 数组,并遍历数组以通过 dstack 追加。

my_array = []

my_array_3d = np.reshape(my_array, (my_array.shape[0], 1, my_array.shape[1]))

for idx, val in enumerate(my_array_3d ):
    for i in range(idx):
        np.append(my_array_3d , i, axis=2)

我不断收到错误:

ValueError: all the input arrays must have same number of dimensions

所以如果我的数组看起来像这样:

my_array = ([[1,1,1],
             [2,2,2],
             [3,3,3],
             [4,4,4]])

我希望 Z[2] 是:

[[1,1,1],
[2,2,2]]

我希望 Z[3] 是:

[[1,1,1],
 [2,2,2],
 [3,3,3]]

标签: pythonnumpy

解决方案


推荐阅读