简体   繁体   English

连接具有不同第一维的二维数组

[英]Concatenate 2D arrays with different first dimension

I have three numpy arrays, respectively with shape:我有三个 numpy 数组,分别具有以下形状:

x1 = (30, 17437)
x2 = (30, 24131)
x3 = (30, 19782)

I would like to concatenate them and create a numpy array of dimension (30, 61350) .我想连接它们并创建一个维度为(30, 61350)的 numpy 数组。 I tried with我试过

labels = np.concatenate((x1, x2, x3))

but I got the error:但我得到了错误:

all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 17437 and the array at index 1 has size 24131串联轴的所有输入数组维度必须完全匹配,但沿维度 1,索引 0 处的数组大小为 17437,索引 1 处的数组大小为 24131

You can do it as shown below:您可以按如下所示进行操作:

labels = np.array([x1[0], (x1[1] + x2[1] + x3[1])])
print(labels)

Output:输出:

[   30 61350]

You can use numpy.r_ :您可以使用numpy.r_

x1 = np.zeros((30, 17437))
x2 = np.zeros((30, 24131))
x3 = np.zeros((30, 19782))
np.r_['-1',x1,x2,x3]

Check:查看:

>>> np.r_['-1',x1,x2,x3].shape
(30, 61350)

You forgot to specify the axis along which the arrays will be joined.您忘记指定连接数组的axis This issue is fixed easily:这个问题很容易解决:

labels = np.concatenate((x1, x2, x3), axis=1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM