In this article we will discuss how to convert a 1D Numpy Array to a 2D numpy array or Matrix using reshape() function. We will also discuss how to construct the 2D array row wise and column wise, from a 1D array.

Suppose we have a 1D numpy array of size 10,

Output:

Now we want to convert it to a 2D numpy array or matrix of shape 2X5 i.e. 2 rows and 5 columns like this,

Reshape 1D array to 2D array or Matrix

First, import the numpy module,

Now to convert the shape of numpy array, we can use the reshape() function of the numpy module,

numpy.reshape()

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 new shape.

Let’s use this to convert our 1D numpy array to 2D numpy array,

Output:

We passed the 1D array as the first argument and the new shape i.e. a tuple (2, 5) as the second argument. It returned a 2D view of the passed array.

An important point here is that the new shape of the array must be compatible with the original shape of the input array, otherwise it will raise the ValueError. For example, if we try to reshape out 1D numpy array of 10 elements to a 2D array of size 2X3, then it will raise error,

Error:

It raised the error because 1D array of size 10 can only be reshaped to a 2D array of size 2X5 or 5X2. But in the above example, we tried to convert it into a shape which is incompatible with its size.

Reshaped 2D array is a view of 1D array

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

Output:

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

Suppose we have a 1D numpy array of 12 elements,

Now let’s convert this 1D array to a 3D numpy array i.e.

Output:

We passed the 1D array as the first argument and the new shape i.e. a tuple (2, 2, 3) as the second argument. It returned a 3D view of the passed array.

Convert 1D Numpy array to a 2D numpy array along the column

In the previous example, when we converted a 1D array to a 2D array or matrix, then the items from input array will be read row wise i.e.

  • 1st row of 2D array was created from items at index 0 to 2 in input array
  • 2nd row of 2D array was created from items at index 3 to 5 in input array
  • 3rd row of 2D array was created from items at index 6 to 8 in input array

Now suppose we want to construct the matrix / 2d array column wise. For that we can pass the order parameter as ‘F’ in the reshape() function i.e.

Output:

It converted the 1D array to a 2D matrix and this matrix was created column wise i.e.

  • 1st column of 2D array was created from items at index 0 to 2 in input array
  • 2nd column of 2D array was created from items at index 3 to 5 in input array
  • 3rd column of 2D array was created from items at index 6 to 8 in input array

Convert 2D Array to 1D Array as copy

If possible then numpy.reshape() returns a view of the original array. Now suppose we want to create a 2D copy of the 1D numpy array then use the copy() function along with the reshape() function,

Output:

It created a 2D copy of the 1D array. Any changes made in this 2D array will not affect the original 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.