In this article we will learn about a function flatten() and how we can use it to flatten a numpy array of any shape.

numpy.ndarray.flatten()

In Python’s Numpy module, a numpy array has a member function to flatten its contents i.e. convert array of any shape to a 1D numpy array,

Parameters:

  • order: The order in which items from numpy array will be used,
  • ‘C’: Read items from array row wise i.e. using C-like index order.
  • ‘F’: Read items from array column wise i.e. using Fortran-like index order.
  • ‘A’: Read items from array based on memory order of items

It returns a copy of the input array but in flattened shape i.e. 1D array.

Let’s understand this with some practical examples,

Flatten a matrix or a 2D array to a 1D array using ndarray.flatten()

First of all, import the numpy module,

Suppose we have a 2D Numpy array,

Output:

Now we want to convert this 2D Numpy array to a flat array i.e. a 1D Numpy array. Let’s see how to do that using flatten() function,

Output:

So, this is how we can use flatten() function to get a flattened 1D copy of a numpy array of any shape.

ndarray.flatten() returns a copy of the input array

flatten() always returns a copy of the input array i.e. any changes done in the returned array will not modify the original array.
Let’s verify this with an example,

Output:

We created a 1D array from a 2D array using flatten() function and then modified the 3rd element in the 1D numpy array. But the changes in this 1D array did not affect the original 2D numpy array. This proves that the returned flattened array is just a copy of the input numpy array.

Flatten a 2D Numpy array along different axis using flatten()

ndarray.flatten() accepts an optional parameter order. It can be ‘C’ or ‘F’ or ‘A’, but the default value is ‘C’.
It tells the order in which items from input numpy array will be used,

  • ‘C’: Read items from array row wise i.e. using C-like index order.
  • ‘F’: Read items from array column wise i.e. using Fortran-like index order.
  • ‘A’: Read items from array based on memory order of items.

Let’s discuss them one by one with examples,

We have a 2D Numpy array,

Flatten 2D array row wise

If it doesn’t pass the order parameter in flatten() function then its default value will be ‘C’. It means elements from a 2D array will be read row by row,

Output:

Flatten 2D array column wise

If pass ‘F’ as the order parameter in flatten() function then it means elements from a 2D array will be read column,

Output:

Flatten 2D array based on memory layout

Let’s create a transpose view of the 2D numpy array,

Output:

Now flatten this transposed view ROW WISE,

Output:

As the order parameter was ‘C’, therefore it read the elements from view object row wise. But the original memory layout view was neglected and the current layout of the view object was used.

Now flatten this transposed view based on memory layout using argument ‘A’

Output:

Instead of considering the current layout in view, it used the memory layout of the original array object to read items row wise.

Flatten a 3D array to 1D numpy array using ndarray.flatten()

Output:

Now let’s flatten this 3D numpy array,

Output:

Flatten a list of arrays using ndarray.flatten()

Let’s create a list of numpy arrays,

Output:

Now convert this list of numpy arrays to a flat 1D numpy array,

Output:

Flatten a list of lists using ndarray.flatten()

Create a 2D numpy array from a list of list and then convert that to a flat 1D Numpy array,

Output:

So, this is how we can use flatten() function in numpy.

The complete example is as follows,

Output

Python Recommendations:

C++ & C++11 Recommendations:

If you didn't find what you were looking, then do suggest us in the comments below. We will be more than happy to add that.

Subscribe with us to join 1500+ Python & C++ developers, to get more Tips &  Tutorials like this.