3

Say I have this object array containing lists of the same length:

>>> a = np.empty(2, dtype=object)
>>> a[0] = [1, 2, 3, 4]
>>> a[1] = [5, 6, 7, 8]
>>> a
array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=object)
  1. How can I convert this to a numeric 2D array?

    >>> a.shape
    (2,)
    >>> b = WHAT_GOES_HERE(a)
    >>> b
    array([[1, 2, 3, 4],
           [5, 6, 7, 8]])
    >>> b.shape
    (2, 4)
    
  2. How can I do the reverse?

  3. Does it get easier if my a array is an np.array of np.arrays, rather than an np.array of lists?

    >>> na = np.empty(2, dtype=object)
    >>> na[0] = np.array([1, 2, 3, 4])
    >>> na[1] = np.array([5, 6, 7, 8])
    >>> na
    array([array([1, 2, 3, 4]), ([5, 6, 7, 8])], dtype=object)
    
1
  • @Divakar: ValueError: setting an array element with a sequence. - astype can't reshape Commented Sep 22, 2016 at 11:16

4 Answers 4

3

One approach using np.concatenate -

b = np.concatenate(a).reshape(len(a),*np.shape(a[0]))

The improvement suggest by @Eric to use *np.shape(a[0]) should make it work for generic ND shapes.

Sample run -

In [183]: a
Out[183]: array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=object)

In [184]: a.shape
Out[184]: (2,)

In [185]: b = np.concatenate(a).reshape(len(a),*np.shape(a[0]))

In [186]: b
Out[186]: 
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])

In [187]: b.shape
Out[187]: (2, 4)

To get back a, it seems we can use a two-step process, like so -

a_back = np.empty(b.shape[0], dtype=object)
a_back[:] = b.tolist()

Sample run -

In [190]: a_back = np.empty(b.shape[0], dtype=object)
     ...: a_back[:] = b.tolist()
     ...: 

In [191]: a_back
Out[191]: array([[1, 2, 3, 4], [5, 6, 7, 8]], dtype=object)

In [192]: a_back.shape
Out[192]: (2,)
Sign up to request clarification or add additional context in comments.

3 Comments

Nice concise solution for the reverse - I was thinking I might have to resort to a for loop
Replacing the -1 with np.shape(a[0]) makes this work for the more general 1D of ND -> N+1D case too
@Eric Thanks for the improvement! Updated the post.
2

You canuse np.vstack():

>>> a = np.vstack(a).astype(int)

3 Comments

I think the astype() is unecessary too!
yes, you are right, although you didn't explicitly say you wanted int in the end (although - what esle in this example...), so better leave it in ;)
I just said numeric, and in this case the dtype is int32 anyway, it seems
0

Here's an approach that converts the source NumPy array to lists and then into the desired NumPy array:

b = np.array([k for k in a])
b
array([[1, 2, 3, 4],
       [5, 6, 7, 8]])
c = np.array([k for k in b], dtype=object)
c
array([[1, 2, 3, 4],
       [5, 6, 7, 8]], dtype=object)

Comments

0

I found that round-tripping through list with np.array(list(a)) was sufficient.

This seems to be equivalent to using np.stack(a).

Both of these have the benefit of working in the more general case of converting a 1D array of ND arrays into an (N+1)D array.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.