Merge numpy arrays that are list items

I have a list containing numpy arrays, such as L = [a, b, c], where a, b and c are numpy arrays with sizes N_a in T, N_b in T and N_c in T.
I want to match the strings a, b and c and get a numpy array with the form (N_a + N_b + N_c, T). Obviously, one of them starts in a for loop and uses numpy.concatenate, but is there any pythonic way to do this?

thanks

+6
source share
2 answers

Use numpy.vstack .

 L = (a,b,c) arr = np.vstack(L) 
+10
source

help('concatenate' has the following signature:

 concatenate(...) concatenate((a1, a2, ...), axis=0) Join a sequence of arrays together. 

(a1, a2, ...) looks like your list, right? And the default axis is the one you want to join. So let's try:

 In [149]: L = [np.ones((3,2)), np.zeros((2,2)), np.ones((4,2))] In [150]: np.concatenate(L) Out[150]: array([[ 1., 1.], [ 1., 1.], [ 1., 1.], [ 0., 0.], [ 0., 0.], [ 1., 1.], [ 1., 1.], [ 1., 1.], [ 1., 1.]]) 

vstack also does this, but look at its code:

 def vstack(tup): return np.concatenate([atleast_2d(_m) for _m in tup], 0) 

All he does is make sure that the component arrays have 2 dimensions that you make.

+2
source

Source: https://habr.com/ru/post/981485/


All Articles