# correct and efficient way to flatten array in numpy in python?

## Question:

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.

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]])
``````

``````>>> 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]])
``````

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.

``````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

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.