如何沿x轴连接4个numpy矩阵?

时间:2020-07-10 05:25:37

标签: python python-3.x numpy valueerror

我正在尝试沿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)

2 个答案:

答案 0 :(得分:1)

我建议您使用numpy concatenate 。我使用此方法将两个图像合并为一个图像。它为您提供了在X轴和Y轴这两个轴上串联的选项。有关此的更多信息,请访问link

答案 1 :(得分: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,删除 每个(一个)依次。

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

然后,您应该仔细查看此数组,并找出问题所在。