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).
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With