How do I convert an array of arrays into a multi-dimensional array in Python? -


i have numpy array (of length x) of arrays, of of same length (y), has type "object" , has dimension (x,). "convert" array of dimension (x, y) type of elements of member arrays ("float").

the way can see "manually" like

[x x in my_array] 

is there better idiom accomplishing "conversion"?


for example have like:

array([array([ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),        array([ 0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]),        array([ 0.,  0.,  1.,  0.,  0.,  0.,  0.,  0.,  0.,  0.]), ...,        array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.]),        array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.,  0.,  0.]),        array([ 0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  0.,  1.])], dtype=object) 

which has shape (x,) rather (x, 10).

you can concatenate arrays on new axis. example:

in [1]: a=np.array([1,2,3],dtype=object)    ...: b=np.array([4,5,6],dtype=object) 

to make array of arrays can't combine them array, deleted answer did:

in [2]: l=np.array([a,b]) in [3]: l out[3]:  array([[1, 2, 3],        [4, 5, 6]], dtype=object) in [4]: l.shape out[4]: (2, 3) 

instead have create empty array of right shape, , fill it:

in [5]: arr = np.empty((2,), object) in [6]: arr[:]=[a,b] in [7]: arr out[7]: array([array([1, 2, 3], dtype=object),                 array([4, 5, 6], dtype=object)],                dtype=object) 

np.stack acts np.array, uses concatenate:

in [8]: np.stack(arr) out[8]:  array([[1, 2, 3],        [4, 5, 6]], dtype=object) in [9]: _.astype(float) out[9]:  array([[ 1.,  2.,  3.],        [ 4.,  5.,  6.]]) 

we use concatenate, hstack or vstack combine arrays on different axes. treat array of arrays list of arrays.

if arr 2d (or higher) have ravel first.


Comments