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 OverflowYou 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]])
Python 3.5: Flatten out array then reconstruct array
Flatten array recursively in python - Stack Overflow
Python Challenge - Flatten an Array
Question regarding numpy.flatten()
Videos
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.
You can write a recursive function in order to flatten the list.
def flatten(permutations):
flattens = []
for permutation in permutations:
if len(permutation) == 2:
flattens.extend([[permutation[0], *j] for j in flatten(permutation[1])])
else:
flattens.extend(permutation)
return flattens
if __name__ == '__main__':
permutations = [['a', [['b', ['c']], ['c', ['b']]]], ['b', [['a', ['c']], ['c', ['a']]]], ['c', [['a', ['b']], ['b', ['a']]]]]
print(flatten(permutations))
Output:
[['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]
Slightly shorter recursive solution with a generator:
data = [['a', [['b', ['c']], ['c', ['b']]]], ['b', [['a', ['c']], ['c', ['a']]]], ['c', [['a', ['b']], ['b', ['a']]]]]
def full_combos(d, c = []):
if all(not isinstance(j, list) for j in d):
yield from [c+[j] for j in d]
else:
yield from [j for a, b in d for j in full_combos(b, c+[a])]
print(list(full_combos(data)))
Output:
[['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]