38

I have:

a = array([[1,2,3],[4,5,6]])

and I'd like to flatten it, joining the two inner lists into one flat array entry. I can do:

array(list(flatten(a)))

but that seems inefficient due to the list cast (I want to end up with an array and not a generator.)

Also, how can this be generalized to an array like this:

b = array([[[1,2,3],[4,5,6]], [[10,11,12],[13,14,15]]])

where the result should be:

b = array([[1,2,3,4,5,6],
           [10,11,12,13,14,15]])

are there builtin/efficient numpy/scipy operators for this? thanks.

4 Answers4

43

You might need to check out numpy.flatten and numpy.ravel, both return a 1-d array from an n-d array.

Furthermore, if you're not going to modify the returned 1-d array, I suggest you use numpy.ravel, since it doesn't make a copy of the array, but just return a view of the array, which is much faster than numpy.flatten.

>>>a = np.arange(10000).reshape((100,100))

>>>%timeit a.flatten()
100000 loops, best of 3: 4.02 µs per loop

>>>%timeit a.ravel()
1000000 loops, best of 3: 412 ns per loop

Also check out this post.

Alcott
  • 17,905
  • 32
  • 116
  • 173
22

You can use the reshape method.

>>> import numpy
>>> b = numpy.array([[[1,2,3],[4,5,6]], [[10,11,12],[13,14,15]]])
>>> b.reshape([2, 6])
array([[ 1,  2,  3,  4,  5,  6],
       [10, 11, 12, 13, 14, 15]])
Matteo Italia
  • 123,740
  • 17
  • 206
  • 299
12

How about:

>>> import numpy as np
>>> a=np.arange(1,7).reshape((2,3))
>>> a
array([[1, 2, 3],
       [4, 5, 6]])
>>> a.flatten()
array([1, 2, 3, 4, 5, 6])

and

>>> import numpy as np
>>> b=np.arange(1,13).reshape((2,2,3))
>>> b
array([[[ 1,  2,  3],
        [ 4,  5,  6]],

       [[ 7,  8,  9],
        [10, 11, 12]]])
>>> b.reshape((2,6))
array([[ 1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12]])
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
  • +1 for flatten() -- it can also do Fortran/column-major flattening. reshape(-1) will also flatten. – Dave X Oct 29 '13 at 19:50
-1
a = np.arange(10000)

%timeit a.reshape(100,100)
1000000 loops, best of 3: 517 ns per loop

%timeit a.resize(100,100)
1000000 loops, best of 3: 428 ns per loop

I wonder reshape should take far lesser time but its almost similar

Raidri
  • 17,258
  • 9
  • 62
  • 65