4

I have two arrays A = [a1, ..., an] and B = [b1, ..., bn]. I want to get new matrix C that is equal to

[[a1, b1],
 [a2, b2],
 ...
 [an, bn]]

How can I do it using numpy.concatenate?

3
  • 1
    Look for np.column_stack. Commented Mar 20, 2017 at 17:09
  • @Divakar Nice! Any intuitions why np.column_stack is little bit slower please? Commented Mar 20, 2017 at 19:41
  • 1
    @kmario23 Well it uses np.concatenate under the hoods, so if you are comparing against it, its the function wrapper overhead that we are paying for there. Commented Mar 20, 2017 at 19:42

3 Answers 3

5

How about this very simple but fastest solution ?

In [73]: a = np.array([0, 1, 2, 3, 4, 5])
In [74]: b = np.array([1, 2, 3, 4, 5, 6])
In [75]: ab = np.array([a, b])
In [76]: c = ab.T

In [77]: c
Out[77]: 
array([[0, 1],
       [1, 2],
       [2, 3],
       [3, 4],
       [4, 5],
       [5, 6]])

But, as Divakar pointed out, using np.column_stack gives the answer directly like:

In [85]: np.column_stack([a, b])
Out[85]: 
array([[0, 1],
       [1, 2],
       [2, 3],
       [3, 4],
       [4, 5],
       [5, 6]])

Efficiency (in descending order)

Interestingly, my simple solution turns out to be the fastest. (little faster than np.concatenate, twice as fast as np.column_stack and thrice as fast as np.vstack)

In [86]: %timeit np.array([a, b]).T
100000 loops, best of 3: 4.44 µs per loop

In [87]: %timeit np.concatenate((a[:,None], b[:,None]), axis=1)
100000 loops, best of 3: 5.6 µs per loop

In [88]: %timeit np.column_stack([a, b])
100000 loops, best of 3: 9.5 µs per loop

In [89]: %timeit np.vstack((a, b)).T
100000 loops, best of 3: 14.7 µs per loop
Sign up to request clarification or add additional context in comments.

Comments

2

You can also use np.vstack then transpose the matrix after

import numpy as np
A = [1, 2, 3]
B = [4, 5, 6]
C = np.vstack((A, B)).T

3 Comments

Since it was asked to use np.concatenate I'll just add C = np.concatenate((A, B)).reshape(2,3).T here.
oh, that's right! thanks @Michael for updating the solution
Don't get me wrong, I think your solution is better ;-)
0
In [26]: A=np.arange(5)
In [27]: B=np.arange(10,15)
In [28]: np.concatenate((A[:,None], B[:,None]), axis=1)
Out[28]: 
array([[ 0, 10],
       [ 1, 11],
       [ 2, 12],
       [ 3, 13],
       [ 4, 14]])
In [29]: _.tolist()
Out[29]: [[0, 10], [1, 11], [2, 12], [3, 13], [4, 14]]

np.column_stack, np.vstack, np.stack all do the same thing, just expanding the dimensions of the arrays in different ways.

np.stack((A,B),-1) expands the arrays like I did, with a newaxis indexing.

np.column_stack((A,B)) uses:

arr = array(arr, copy=False, subok=True, ndmin=2).T

np.vstack((A,B)).T uses:

concatenate([atleast_2d(_m) for _m in tup], 0)

As a matter of curiosity, note this vstack equivalent:

In [38]: np.concatenate((A[None],B[None]))
Out[38]: 
array([[ 0,  1,  2,  3,  4],
       [10, 11, 12, 13, 14]])

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.