In this article we will discuss different ways to convert a 2D numpy array or Matrix to a 1D Numpy Array.

First, import the numpy module,

Convert 2D Numpy array / Matrix to a 1D Numpy array using flatten()

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

Parameters:

  • order: The order in which items from the numpy array will be read.
    • ‘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 examples,

Suppose we have a 2D Numpy array or matrix,

Output:

Lets use this to convert a 2D numpy array or matrix to a new flat 1D numpy array,

Output:

The flatten() function always returns a flat copy of the input array. So, any changes made in this new 1D array will not affect the original 2D numpy array.For example,

Output:

We modified the flat array by changing the value at index 0. But this change didn’t affect the original input array.

Know more about the flatten() function.

Convert 2D Numpy array to 1D Numpy array using numpy.ravel()

Python’s numpy module provides a built-in function that accepts an array-like element as parameter and returns a flatten 1D view of the input array,

input_arr can be of any shape, but numpy.ravel() function returns a 1D view of it. Let’s use this to convert our 2D array to 1D array,

Output:

In most scenarios, ravel() returns a view of the input array. Therefore if we do any changes in the returned 1D array then it will be reflected in the original input array too. For example,

Output:

We modified the 2nd element in the 1D array but it also modified the original 2D input array.

Know more about numpy.ravel() function like how to check if ravel() returned a view or copy and different possible values in order parameter.

Convert a 2D Numpy array to 1D array using numpy.reshape()

Python’s numpy module provides a built-in function reshape() to convert the shape of a numpy array,

numpy.reshape(arr, newshape, order=’C’)

It accepts following arguments,

  • a: Array to be reshaped, it can be a numpy array of any shape or a list or list of lists.
  • newshape: New shape either be a tuple or an int.
  • order: The order in which items from the input array will be used.

It returns a new view object (if possible, otherwise returns a copy) of the array with the new shape.

Let’s use this to convert our 2D array or matrix to a 1D array,

Output:

As there were a total 9 elements (3X3) in the 2D input array, therefore we passed the 9 as the second argument in the reshape() function. If you pass the wrong size in the reshape() function i.e. size which is not compatible then it will raise ValueError. For example, if we try to convert 3X3 Matrix / 2D numpy array to a 1D array of shape / size 7 then it will raise error,

Error:

Therefore it is necessary to pass the correct size.

numpy.reshape() and -1 size

But there might be scenarios when the input array is too big and multidimensional or we just don’t know the total elements in the input array. But we want to convert that to a 1D array. In such scenarios we can pass the size as -1,

Output:

It will convert the input array of any shape to a 1D array.

numpy.reshape() returns a new view object if possible

If possible then reshape() function returns a view of the input array and any modification done in the view object will be reflected in the original input array too. For example,

Output:

Convert 2D Numpy array to 1D array but column wise

Output:

If we pass the order parameter in reshape() function as “F” then it will read 2D input array column wise.

So, these were the 3 ways to convert a 2D Numpy Array or Matrix to a 1D Numpy Array.

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.