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.
Answer from Alcott on Stack OverflowVideos
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.
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]])
A list of lists named xss can be flattened using a nested list comprehension:
flat_list = [
x
for xs in xss
for x in xs
]
The above is equivalent to:
flat_list = []
for xs in xss:
for x in xs:
flat_list.append(x)
Here is the corresponding function:
def flatten(xss):
return [x for xs in xss for x in xs]
This is the fastest method.
As evidence, using the timeit module in the standard library, we see:
$ python -mtimeit -s'xss=[[1,2,3],[4,5,6],[7],[8,9]]*99' '[x for xs in xss for x in xs]'
10000 loops, best of 3: 143 usec per loop
$ python -mtimeit -s'xss=[[1,2,3],[4,5,6],[7],[8,9]]*99' 'sum(xss, [])'
1000 loops, best of 3: 969 usec per loop
$ python -mtimeit -s'xss=[[1,2,3],[4,5,6],[7],[8,9]]*99' 'reduce(lambda xs, ys: xs + ys, xss)'
1000 loops, best of 3: 1.1 msec per loop
Explanation: the methods based on + (including the implied use in sum) are, of necessity, O(L**2) when there are L sublists -- as the intermediate result list keeps getting longer, at each step a new intermediate result list object gets allocated, and all the items in the previous intermediate result must be copied over (as well as a few new ones added at the end). So, for simplicity and without actual loss of generality, say you have L sublists of M items each: the first M items are copied back and forth L-1 times, the second M items L-2 times, and so on; total number of copies is M times the sum of x for x from 1 to L excluded, i.e., M * (L**2)/2.
The list comprehension just generates one list, once, and copies each item over (from its original place of residence to the result list) also exactly once.
You can use itertools.chain():
>>> import itertools
>>> list2d = [[1,2,3], [4,5,6], [7], [8,9]]
>>> merged = list(itertools.chain(*list2d))
Or you can use itertools.chain.from_iterable() which doesn't require unpacking the list with the * operator:
>>> import itertools
>>> list2d = [[1,2,3], [4,5,6], [7], [8,9]]
>>> merged = list(itertools.chain.from_iterable(list2d))
This approach is arguably more readable than [item for sublist in l for item in sublist] and appears to be faster too:
$ python3 -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99;import itertools' 'list(itertools.chain.from_iterable(l))'
20000 loops, best of 5: 10.8 usec per loop
$ python3 -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' '[item for sublist in l for item in sublist]'
10000 loops, best of 5: 21.7 usec per loop
$ python3 -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99' 'sum(l, [])'
1000 loops, best of 5: 258 usec per loop
$ python3 -mtimeit -s'l=[[1,2,3],[4,5,6], [7], [8,9]]*99;from functools import reduce' 'reduce(lambda x,y: x+y,l)'
1000 loops, best of 5: 292 usec per loop
$ python3 --version
Python 3.7.5rc1
I have a numpy array with initial dimensions of [605, 700, 3]. I then reshape the array such that the dimensions are [(605*700), 3] (basically a flat list of 3 item tuples). The array is flattened so that it can be fed into a variety of analysis functions. However, I ultimately need to reconstruct the array so that it is [605, 700, 3]. How would I go about doing this?
EDIT: I forgot to mention that I need the values to return to their starting indices when the array is reconstructed.