Hesham Eraqi Hesham Eraqi - 21 days ago 4x
Python Question

Converting List of Multidimensional Arrays to Single Multidimensional Array?

Let Y be a list of 100 ndarrays, such that Y[i] is an ndarray of an image, its shape is 160x320x3.

I want X no be an ndarrays that contains all the images, I do as follows:

x = [ y[i] for i in range(0,10) ]

But it produces a list of of 100 160X320X3 ndarrays. How can I modify it to get an ndarray of shape 100x160x320x3 ?


Calling np.array on Y (i.e np.array(Y)) should turn the list of ndarrays into one ndarray, with the size of the first axis corresponding to the length of the list.


>>> x = np.array([[1,2], [3,4]])
>>> c = [x,x] # list of 2x2 arrays
>>> c
[array([[1, 2],
       [3, 4]]), 
array([[1, 2],
       [3, 4]])]
>>> np.array(c) # 2x2x2 array
array([[[1, 2],
        [3, 4]],

       [[1, 2],
        [3, 4]]])

Just call np.array on Y, or x if you only want a slice of Y.