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,

# create 1D numpy array from a list arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print('1D Numpy array:') print(arr)

Output:

[0 1 2 3 4 5 6 7 8 9]

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,

[[0 1 2 3 4] [5 6 7 8 9]]

## Reshape 1D array to 2D array or Matrix

First, import the numpy module,

import numpy as np

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

### Frequently Asked:

- Python: Convert Matrix / 2D Numpy Array to a 1D Numpy Array
- Python: Convert a 1D array to a 2D Numpy array or Matrix
- Convert a Pandas Series or Index to a NumPy array
- Convert an PIL image to a NumPy Array in Python

**numpy.reshape()**

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

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,

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # Convert 1D array to a 2D numpy array of 2 rows and 3 columns arr_2d = np.reshape(arr, (2, 5)) print(arr_2d)

Output:

[[0 1 2 3 4] [5 6 7 8 9]]

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,

# Converting 1D array to a 2D numpy array of incompatible shape will cause error arr_2d = np.reshape(arr, (2, 3))

Error:

ValueError: cannot reshape array of size 10 into shape (2,3)

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,

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) arr_2d = np.reshape(arr, (2, 5)) # Modify the 2D numpy array (View object) arr_2d[0][0] = 22 print('1D Numpy array:') print(arr) print('2D Numpy array:') print(arr_2d)

Output:

1D Numpy array: [22 1 2 3 4 5 6 7 8 9] 2D Numpy array: [[22 1 2 3 4] [ 5 6 7 8 9]]

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

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

arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12])

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

# Convert 1D Numpy array to a 3D array with 2 matrices of shape 2X3 arr_3d = np.reshape(arr, (2, 2, 3)) print('3D Numpy array:') print(arr_3d)

Output:

3D Numpy array: [[[ 1 2 3] [ 4 5 6]] [[ 7 8 9] [10 11 12]]]

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.

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) # column wise conversion of 1D numpy array to 2D Numpy array arr_2d = np.reshape(arr, (2, 5), order='F') print('2D Numpy array:') print(arr_2d)

Output:

2D Numpy array: [[0 2 4 6 8] [1 3 5 7 9]]

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,

arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) arr_2d = np.reshape(arr, (2, 5)).copy() # Modify the 2D numpy array and it will not affect original 1D array arr_2d[0][0] = 22 print('1D Numpy array:') print(arr) print('2D Numpy array:') print(arr_2d)

Output:

1D Numpy array: [0 1 2 3 4 5 6 7 8 9] 2D Numpy array: [[22 1 2 3 4] [ 5 6 7 8 9]]

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,

import numpy as np def main(): print('*** Convert a 1D array to a 2D Numpy array ***') # create 1D numpy array from a list arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print('1D Numpy array:') print(arr) # Convert 1D array to a 2D numpy array of 2 rows and 3 columns arr_2d = np.reshape(arr, (2, 5)) print('2D Numpy array:') print(arr_2d) print('Shape of 2D array must be compatible to 1D array') # Converting 1D array to a 2D numpy array of incompatible shape will cause error #arr_2d = np.reshape(arr, (2, 3)) #ValueError: cannot reshape array of size 10 into shape (2,3) print('Reshaped 2D array is a view of 1D array') # Modify the 2D numpy array (View object) arr_2d[0][0] = 22 print('1D Numpy array:') print(arr) print('2D Numpy array:') print(arr_2d) print('Convert a 1D Numpy array to a 3D Numpy array using numpy.reshape()') arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]) print('1D Numpy array:') print(arr) # Convert 1D Numpy array to a 3D array with 2 matrices of shape 2X3 arr_3d = np.reshape(arr, (2, 2, 3)) print('3D Numpy array:') print(arr_3d) print('*** Convert 1D Numpy array to 2D numpy array along the column ***') arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print('1D Numpy array:') print(arr) # column wise conversion of 1D numpy array to 2D Numpy array arr_2d = np.reshape(arr, (2, 5), order='F') print('2D Numpy array:') print(arr_2d) print('*** Convert 2D aray to 1D array as copy ***') arr = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) print('1D Numpy array:') print(arr) arr_2d = np.reshape(arr, (2, 5)).copy() print('2D Numpy array:') print(arr_2d) # Modify the 2D numpy array and it will not affect original 1D array arr_2d[0][0] = 22 print('1D Numpy array:') print(arr) print('2D Numpy array:') print(arr_2d) if __name__ == '__main__': main()

Output

*** Convert a 1D array to a 2D Numpy array *** 1D Numpy array: [0 1 2 3 4 5 6 7 8 9] 2D Numpy array: [[0 1 2 3 4] [5 6 7 8 9]] Shape of 2D array must be compatible to 1D array Reshaped 2D array is a view of 1D array 1D Numpy array: [22 1 2 3 4 5 6 7 8 9] 2D Numpy array: [[22 1 2 3 4] [ 5 6 7 8 9]] Convert a 1D Numpy array to a 3D Numpy array using numpy.reshape() 1D Numpy array: [ 1 2 3 4 5 6 7 8 9 10 11 12] 3D Numpy array: [[[ 1 2 3] [ 4 5 6]] [[ 7 8 9] [10 11 12]]] *** Convert 1D Numpy array to 2D numpy array along the column *** 1D Numpy array: [0 1 2 3 4 5 6 7 8 9] 2D Numpy array: [[0 2 4 6 8] [1 3 5 7 9]] *** Convert 2D aray to 1D array as copy *** 1D Numpy array: [0 1 2 3 4 5 6 7 8 9] 2D Numpy array: [[0 1 2 3 4] [5 6 7 8 9]] 1D Numpy array: [0 1 2 3 4 5 6 7 8 9] 2D Numpy array: [[22 1 2 3 4] [ 5 6 7 8 9]]