0

我正在尝试沿 x 轴连接 4 个 numpy 矩阵。下面是我写的代码。

print(dt.shape)
print(condition.shape)
print(uc.shape)
print(rt.shape)

x = np.hstack((dt, condition, uc, rt))

print(x.shape)

我得到以下输出。

(215063, 1)
(215063, 1112)
(215063, 1)
(215063, 1)

我收到以下错误。

ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 2 dimension(s) and the array at index 1 has 1 dimension(s)

最终输出应该是

(215063, 1115)
4

2 回答 2

1

我会推荐你​​使用 numpy concatenate。我用它来将两个图像合并到一个图像中。它为您提供了在 X 和 Y 两个轴中的任何一个上连接的选项。有关此链接的更多信息,请访问此链接

于 2020-07-10T05:44:44.403 回答
0

你的代码没问题。为了确认这一点,我对较小的阵列进行了以下测试:

dt = np.arange(1,6).reshape(-1,1)
condition = np.arange(11,41).reshape(-1,6)
uc = np.arange(71,76).reshape(-1,1)
uc = np.arange(81,86).reshape(-1,1)
print(dt.shape, condition.shape, uc.shape, rt.shape)
x = np.hstack((dt, condition, uc, rt))
print(x.shape)
print(x)

并得到:

(5, 1) (5, 6) (5, 1) (5, 1)
(5, 9)
[[ 1 11 12 13 14 15 16 81 41]
 [ 2 17 18 19 20 21 22 82 42]
 [ 3 23 24 25 26 27 28 83 43]
 [ 4 29 30 31 32 33 34 84 44]
 [ 5 35 36 37 38 39 40 85 45]]

因此,您的数据可能有问题。

尝试np.hstack在上述数组集上运行,依次删除每个(一个)数组。

如果在一种情况下(没有一些数组)执行成功,那么问题的根源就是在这种情况下缺少数组。

然后你应该彻底查看这个数组并找出它有什么问题。

于 2020-07-10T09:08:22.810 回答