python - Convert a list of 2D numpy arrays to one 3D numpy array? -
i have list of several hundred 10x10 arrays want stack single nx10x10 array. @ first tried simple
newarray = np.array(mylist)
but returned "valueerror: setting array element sequence."
then found online documentation dstack(), looked perfect: "...this simple way stack 2d arrays (images) single 3d array processing." i'm trying do. however,
newarray = np.dstack(mylist) tells me "valueerror: array dimensions must agree except d_0", odd because arrays 10x10. thought maybe problem dstack() expects tuple instead of list,
newarray = np.dstack(tuple(mylist)) produced same result.
at point i've spent 2 hours searching here , elsewhere find out i'm doing wrong and/or how go correctly. i've tried converting list of arrays list of lists of lists , 3d array, didn't work either (i ended lists of lists of arrays, followed "setting array element sequence" error again).
any appreciated.
newarray = np.dstack(mylist) should work. example:
import numpy np # here list of 5 10x10 arrays: x=[np.random.random((10,10)) _ in range(5)] y=np.dstack(x) print(y.shape) # (10, 10, 5) # shape nx10x10, use rollaxis: y=np.rollaxis(y,-1) print(y.shape) # (5, 10, 10)
Comments
Post a Comment