numpy数组连接:"ValueError:所有输入数组必须具有相同数量的维度"

35

如何连接这些 numpy 数组?

第一个 np.array 具有形状为 (5,4)

[[  6487    400 489580      0]
 [  6488    401 492994      0]
 [  6491    408 489247      0]
 [  6491    408 489247      0]
 [  6492    402 499013      0]]

形状为(5,)的第二个np.array

[  16.   15.   12.  12.  17. ]

最终结果应该是

[[  6487    400    489580    0   16]
 [  6488    401    492994    0   15]
 [  6491    408    489247    0   12]
 [  6491    408    489247    0   12]
 [  6492    402    499013    0   17]]
我尝试了np.concatenate([array1,array2]),但是出现了这个错误

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

我做错了什么?

这第二个数组怎么可能有形状为(1,)的?这里是否存在一些关于对象数组的奇怪问题? - user2357112
这是我运行 array2.shape 得到的结果。 - RaduS
那么你的数组肯定出了什么问题,你需要找出问题所在。 - user2357112
就在那之前,我运行了这个代码 array2 = np.array(np.round(data[:,0]/20)) - RaduS
4个回答

50

为了使用np.concatenate,我们需要将第二个数组扩展为2D,然后沿着axis=1连接 -

np.concatenate((a,b[:,None]),axis=1)

或者,我们可以使用np.column_stack来处理它 -

np.column_stack((a,b))

示例运行:

In [84]: a
Out[84]: 
array([[54, 30, 55, 12],
       [64, 94, 50, 72],
       [67, 31, 56, 43],
       [26, 58, 35, 14],
       [97, 76, 84, 52]])

In [85]: b
Out[85]: array([56, 70, 43, 19, 16])

In [86]: np.concatenate((a,b[:,None]),axis=1)
Out[86]: 
array([[54, 30, 55, 12, 56],
       [64, 94, 50, 72, 70],
       [67, 31, 56, 43, 43],
       [26, 58, 35, 14, 19],
       [97, 76, 84, 52, 16]])
如果b是一个dtype=object1D数组,其形状为(1,),那么很可能所有数据都包含在其中唯一的元素中,我们需要在连接之前将其展平。为此,我们也可以使用np.concatenate。以下是一个示例运行以说明这一点 -
In [118]: a
Out[118]: 
array([[54, 30, 55, 12],
       [64, 94, 50, 72],
       [67, 31, 56, 43],
       [26, 58, 35, 14],
       [97, 76, 84, 52]])

In [119]: b
Out[119]: array([array([30, 41, 76, 13, 69])], dtype=object)

In [120]: b.shape
Out[120]: (1,)

In [121]: np.concatenate((a,np.concatenate(b)[:,None]),axis=1)
Out[121]: 
array([[54, 30, 55, 12, 30],
       [64, 94, 50, 72, 41],
       [67, 31, 56, 43, 76],
       [26, 58, 35, 14, 13],
       [97, 76, 84, 52, 69]])

2
b[:, None] 表示法很好,但也值得提到 b.reshape()。 - Andrew
1
@PaulPanzer 这值得一篇新的帖子!考虑发一下吧。 - Divakar
第二个数组的形状是由于这个代码 array2 = np.array(np.round(data[:,0]/20)) 导致的,我用 array2 = np.array(np.round(data[:,0]/20)).astype(int) 进行了修复。 - RaduS

4

还有 np.c_

>>> a = np.arange(20).reshape(5, 4)
>>> b = np.arange(-1, -6, -1)
>>> a
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11],
       [12, 13, 14, 15],
       [16, 17, 18, 19]])                                                                                                                                   
>>> b                                                                                                                                                       
array([-1, -2, -3, -4, -5])                                                                                                                                 
>>> np.c_[a, b]
array([[ 0,  1,  2,  3, -1],          
       [ 4,  5,  6,  7, -2],                       
       [ 8,  9, 10, 11, -3],                      
       [12, 13, 14, 15, -4],                                
       [16, 17, 18, 19, -5]])

敢不敢解码这个:np.r_['1,2,0', a, -1:-6:-1] :) - hpaulj
我想知道np.c_是否总是可以替代np.column_stack - hpaulj
@hpaulj 看起来是这样,但我对 np.column_stack 不是很熟悉。它基本上是一个二维的 concatenate,特殊处理一维输入,对吧? - Paul Panzer

2
您可以这样做。
import numpy as np

x = np.random.randint(100, size=(5, 4))
y = [16, 15, 12, 12, 17]

print(x)

val = np.concatenate((x,np.reshape(y,(x.shape[0],1))),axis=1)
print(val)

这将输出:
[[32 37 35 53]
 [64 23 95 76]
 [17 76 11 30]
 [35 42  6 80]
 [61 88  7 56]]

[[32 37 35 53 16]
 [64 23 95 76 15]
 [17 76 11 30 12]
 [35 42  6 80 12]
 [61 88  7 56 17]]

0

这与原问题无关。 但在循环中垂直添加列时可能会有用。

a = np.empty([0,1], dtype=float) 
b = np.array([[6487, 400, 489.580],
              [6488, 401, 492.994],
              [6491, 408, 489.247],
              [6491, 408, 489.247],
              [6492, 402, 499.013]])

for c in range(0, 3):
    col = b[:, c]
    col = col.reshape(len(col),1)
    a = np.vstack((a, col))

print(a)

----------------------------------------------
Output:
[[6487.   ]
 [6488.   ]
 [6491.   ]
 [6491.   ]
 [6492.   ]
 [ 400.   ]
 [ 401.   ]
 [ 408.   ]
 [ 408.   ]
 [ 402.   ]
 [ 489.58 ]
 [ 492.994]
 [ 489.247]
 [ 489.247]
 [ 499.013]]

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接