Member-only story
5 Ways To Flatten an Array in NumPy
How to process an n-dimensional array as if it was a vector

NumPy, a powerful Python library for scientific computing, has quite a few methods for an operation as simple as flattening, but there’s a good reason. Usually, you don’t need to worry too much about the difference: the recipe “flatten
is always a copy, and reshape(-1)
is a view whenever possible” from “NumPy Illustrated” is enough for most use cases.

Going deeper is usually necessary when you’re fine-tuning the code to make it faster or less memory-hungry or to better understand what’s going on under the hood to avoid hard-to-debug issues related to views and copies.
Scroll down to the conclusion for a brief summary, or read on for details.
A Copy or a View
The flattened array can be a copy or a view of the original. The view operates lazily. It saves memory, but you have to remember that changing the items of the result changes the original array and vice versa. It matters little for smaller arrays, but when you deal with arrays of tens or hundreds of megabytes or larger, the difference in memory…