python - How to "zip" several N-D arrays in Numpy? -


the conditions following:

1) have list of n-d arrays , list of unknown length m

2) dimensions each arrays equal, unknown

3) each array should splitted along 0-th dimension , resulting elements should grouped along 1-st dimension of length m , stacked along 0-th dimension of same length was

4) resulting rank should n+1 , lenght of 1-st dimension should m

above same zip, in world of n-d arrays.

currently following way:

xs = [list of numpy arrays] grs = [] in range(len(xs[0])):    gr = [x[i] x in xs]     gr = np.stack(gr)    grs.append(gr) grs = np.stack(grs) 

can write shorter bulk operations?

update

here want

import numpy np

sz = 2 sh = (30, 10, 10, 3)  xs = [] in range(sz):     xs.append(np.zeros(sh, dtype=np.int))  value = 0  in range(sz):     index, _ in np.ndenumerate(xs[i]):         xs[i][index] = value         value += 1  grs = [] in range(len(xs[0])):    gr = [x[i] x in xs]    gr = np.stack(gr)    grs.append(gr) grs = np.stack(grs)  print(np.shape(grs)) 

this code apparantly works correctly, producing arrays of shape (30, 2, 10, 10, 3). possible avoid loop?

seems need transpose array respect 1st , 2nd dimension; can use swapaxes this:

np.asarray(xs).swapaxes(1,0) 

example:

xs = [np.array([[1,2],[3,4]]), np.array([[5,6],[7,8]])] grs = [] in range(len(xs[0])):     gr = [x[i] x in xs]      gr = np.stack(gr)     grs.append(gr) grs = np.stack(grs)  grs #array([[[1, 2], #        [5, 6]],  #       [[3, 4], #        [7, 8]]])  np.asarray(xs).swapaxes(1,0) #array([[[1, 2], #        [5, 6]],  #       [[3, 4], #        [7, 8]]]) 

Comments

Popular posts from this blog

javascript - Create a stacked percentage column -

Optimising Firebase database by automatically overwriting data -

javascript - Angular UI-Grid customTemplate directive causing rows to load slowly/? -