如何连接两个numpy数组

时间:2018-05-03 12:19:37

标签: python arrays numpy

我有两个分别具有形状(5741,20000)和(5741,11)的numpy数组。如何将它们连接成一个形状数组(5741,2),即新创建的数组的每一行应该包含第一个数组的行和第二个数组的行?!

例如:

A = [(1, 1),
     (2, 2),
     (3, 3)]

B = [(A),
     (B),
     (C)]

new_array = [((1, 1), (A)),
             ((2, 2), (B)),
             ((3, 3), (C))]

2 个答案:

答案 0 :(得分:1)

使用numpy.hstack()

>>> A=np.array([[1,1],[2,2],[3,3]])

>>> B=np.array([[100],[200],[300]])

>>> np.hstack((A,B))
array([[  1,   1, 100],
       [  2,   2, 200],
       [  3,   3, 300]])

答案 1 :(得分:1)

In [65]: A = np.arange(6).reshape(3,2)
In [66]: B = np.arange(3).reshape(3,1)

hstack或在最后一个维度上连接会产生一个(n,3)数组

In [67]: np.concatenate((A,B),axis=1)
Out[67]: 
array([[0, 1, 0],
       [2, 3, 1],
       [4, 5, 2]])

创建包含不同长度子阵列的数组比较棘手。一个好的起点是所需形状的对象数组:

In [68]: C = np.empty((3,2),dtype=object)
In [69]: C[:,0] = A
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-69-928e3f2975cf> in <module>()
----> 1 C[:,0] = A

ValueError: could not broadcast input array from shape (3,2) into shape (3)

但即使将数组复制到对象槽中也很棘手。将列表复制到插槽更容易:

In [70]: C[:,0] = A.tolist()
In [71]: C[:,1] = B.tolist()
In [72]: C
Out[72]: 
array([[list([0, 1]), list([0])],
       [list([2, 3]), list([1])],
       [list([4, 5]), list([2])]], dtype=object)

随着更多的摆弄,我可以把它变成一个数组而不是列表。或者可能是一系列元组?

你真的明白那个(n,2)阵列会涉及什么?

        [((1, 1), (A)),
         ((2, 2), (B)),
         ((3, 3), (C))]

另一种方法是结构化数组:

In [74]: D = np.empty(3, dtype=[('x',int,2), ('y',int)])
In [75]: D['x']=A
In [76]: D['y']=B
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-76-fbab1c580883> in <module>()
----> 1 D['y']=B

ValueError: could not broadcast input array from shape (3,1) into shape (3)
In [77]: D['y']=B.flat
In [78]: D
Out[78]: 
array([([0, 1], 0), ([2, 3], 1), ([4, 5], 2)],
      dtype=[('x', '<i8', (2,)), ('y', '<i8')])

另一种将AB写入C的方法:

In [81]: C[:,1]=B.ravel()
In [83]: for i in range(3): C[i,0]=A[i]
In [84]: C
Out[84]: 
array([[array([0, 1]), 0],
       [array([2, 3]), 1],
       [array([4, 5]), 2]], dtype=object)

或写元组:

In [85]: for i in range(3): C[i,0]=tuple(A[i])
In [87]: for i in range(3): C[i,1]=tuple(B[i])
In [88]: C
Out[88]: 
array([[(0, 1), (0,)],
       [(2, 3), (1,)],
       [(4, 5), (2,)]], dtype=object)