1

I have an array

np.array([[[ 1,  2], [ 3, -4]],[[-1,  0]]], dtype=object)

and I want to flatten it to get something like:

array([1,2,3,-4,-1,0], dtype=int32)

I tried Flatten numpy array but it raises a Value error

To be clear, my array is always an object array consists of multiple 2D and 1D arrays

6
  • You have an array of arrays, is that really what you want? Commented Jul 5, 2019 at 11:05
  • @NilsWerner yes Commented Jul 5, 2019 at 11:06
  • What's the shape? If 1d, try hstack Commented Jul 5, 2019 at 11:32
  • @hpaulj It is an object array consisting of 1d and 2d arrays Commented Jul 5, 2019 at 11:35
  • In newer versions, the print shows which of the elements are arrays, eg. np.array([np.array([1,2,3]), ...]) Your print is ambiguous, but I'm guessing (2,) containing a (2,2) and (1,2). If so then vstack should make a (3,2). Commented Jul 5, 2019 at 11:45

3 Answers 3

2
In [333]: arr = np.array([[[ 1,  2], [ 3, -4]],[[-1,  0]]], dtype=object)                                       
In [334]: arr                                                                                                   
Out[334]: array([list([[1, 2], [3, -4]]), list([[-1, 0]])], dtype=object)
In [335]: arr.shape                                                                                             
Out[335]: (2,)
In [336]: np.vstack(arr)                                                                                        
Out[336]: 
array([[ 1,  2],
       [ 3, -4],
       [-1,  0]])
In [337]: np.vstack(arr).ravel()                                                                                
Out[337]: array([ 1,  2,  3, -4, -1,  0])
Sign up to request clarification or add additional context in comments.

1 Comment

brilliant, works
0

If you dont know the depth of your nestet array you can do it like this:

l = np.array([[[ 1,  2],[ 3, -4]], [-1,  0]])

from collections.abc import Iterable

def flatten(l):
    returnlist=[]
    for elem in l:
        if isinstance(elem, Iterable):
            returnlist.extend(flatten(elem))
        else:
            returnlist.append(elem)

    return returnlist

np.array(flatten(l))

If its 2 dimensional you can go like that post suggest How to make a flat list out of list of lists:

flat_list = [item for sublist in l for item in sublist]

or just use numpys flatten.

By the way, your example is not two dimensional because of those double brackets, thats also why flatten() does not work:

np.array([[[ 1, 2], [ 3, -4 ]],[[-1, 0]]], dtype=object)

2 Comments

Is there any simpler/faster method ?
Better data to start with would make it easier. You have a mix of a numpy array and lists here. But to answer the question: There might be, but at the moment i know for no faster method even though it probably exists.
0

It's nested, but you can use sum() to do this:

nested_list_values = [[[ 1,  2], [ 3, -4]],[[-1,  0]]]
sum(sum(, []), [])

[1, 2, 3, -4, -1, 0]

Or, with itertools.chain() if that feels more natural:

from itertools import chain

nested_list_values = [[[ 1,  2], [ 3, -4]],[[-1,  0]]]
list(chain(*chain(*nested_list_values)))

[1, 2, 3, -4, -1, 0]

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.