Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have a NumPy array (of length X) of arrays, all of which are of the same length (Y), but which has type "object" and thus has dimension (X,). I would like to "convert" this into an array of dimension (X, Y) with the type of the elements of the member arrays ("float").

The only way I can see to do this is "manually" with something like

[x for x in my_array]

Is there a better idiom for accomplishing this "conversion"?


For example I have something 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 than (X, 10).

like image 885
orome Avatar asked Oct 17 '22 08:10

orome


1 Answers

You can concatenate the arrays on a new axis. For example:

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

To make an array of arrays we can't just combine them with array, as the 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 we have to create an empty array of the right shape, and 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 like np.array, but 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 could also use concatenate, hstack or vstack to combine the arrays on different axes. They all treat the array of arrays as a list of arrays.

If arr is 2d (or higher) we have to ravel it first.

like image 186
hpaulj Avatar answered Oct 21 '22 04:10

hpaulj