Combining NumPy arrays Combining NumPy arrays numpy numpy

Combining NumPy arrays


I believe it's vstack you want

p=array_2q=array_2p=numpy.vstack([p,q])


One of the best ways of learning is experimenting, but I would say you want np.vstack although there are other ways of doing the same thing:

a = np.ones((20,100,3))b = np.vstack((a,a)) print b.shape # (40,100,3)

or

b = np.concatenate((a,a),axis=0)

EDIT

Just as a note, on my machine for the sized arrays in the OP's question, I find that np.concatenate is about 2x faster than np.vstack

In [172]: a = np.random.normal(size=(20,100,3))In [173]: c = np.random.normal(size=(20,100,3))In [174]: %timeit b = np.concatenate((a,c),axis=0)100000 loops, best of 3: 13.3 us per loopIn [175]: %timeit b = np.vstack((a,c))10000 loops, best of 3: 26.1 us per loop


Might be worth mentioning that

    np.concatenate((a1, a2, ...), axis=0) 

is the general form and vstack and hstack are specific cases. I find it easiest to just know which dimension I want to stack over and provide that as the argument to np.concatenate.