0

I have two numpy arrays with shapes (5741, 20000) and (5741, 11) respectively. How can I concatenate them into one array of shape (5741, 2), i.e. each row of the newly created array should contain the row of the first array and the row of the second array?!

Ex:

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

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

new_array = [((1, 1), (A)),
             ((2, 2), (B)),
             ((3, 3), (C))]
1
  • 2
    Pretty sure there are 1000 duplicates for this.. Commented May 3, 2018 at 12:21

2 Answers 2

1

Use 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]])
Sign up to request clarification or add additional context in comments.

1 Comment

Consider accepting the answer if it is a solution to your question. Thanks!
1
In [65]: A = np.arange(6).reshape(3,2)
In [66]: B = np.arange(3).reshape(3,1)

hstack or concatenate on last dimension produces a (n,3) array

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

To create an array that contains subarrays of differing length is trickier. A good starting point is an object array of the desired shape:

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)

But even with that copying arrays into the object slots can be tricky. Copying lists to the slots is easier:

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)

With a bit more fiddling I could make that into an array of arrays rather than list. Or maybe array of tuples?

Do you really understand what that (n,2) array will be involve?

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

Another approach is a structured array:

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')])

Another way of writing A and B to 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)

or writing tuples:

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.