In this article we will discuss the numpy.ravel() function and how we can use it in different ways to flatten a multidimensional numpy array.

numpy.ravel()

Python’s numpy module provides a built-in function,

Parameters:

  • a : array_like
    • It can be a numpy array or any other array-like sequence like list. Elements from it will be read based on given order.
  • 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.
    • ‘K’: Read items from array based on memory order of items

It returns a flattened 1D view of the input array.

Let’s understand it with some examples,

First of all, import the numpy module,

Flatten a matrix or 2D array to 1D array using numpy.ravel()

Suppose we have a 2D Numpy array,

Output:

Let’s generate a flattened 1D view of this 2D numpy array using ravel() function,

Output:

We didn’t provide any order parameter therefore the default value of order parameter ‘C’ was used and elements from the 2D array were read row by row.

numpy.ravel() returns a view

In the previous example, we created a flattened view flat_array of the original 2D numpy array arr_2d. Now let’s modify this view object,

We changed the content of view object only, but the changes will be reflected in both the flattened 1D view object and original 2D numpy array,

Output:

How to access the original array from the flattened view object ?

Returned flattened view object has an attribute base, which points to the original numpy array,

Output:

Use numpy.ravel() along different axis with order parameter

ndarray.ravel() 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,

Get Flatten view of 2D array Row wise

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

Output:

Get Flatten view of 2D array Column wise

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

Output:

Get Flatten view of 2D array based on memory layout

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

Output:

Now get a flatten view this transpose 2D array 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 get flatten view of this transposed numpy array 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 list of lists using numpy.ravel()

numpy.ravel() expects an array like parameter, from which it will create a flattened view. So, instead of a numpy array we can also pass list or list of lists in the ravel() function directly.

Suppose we have a list of lists,

Now let’s create a flattened numpy array from this list of list,

Output:

We can also convert this flattened numpy array to a list,

Output:

So, basically we converted a list of lists to a flat list using numpy.ravel().

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.