NumPy Reshape: Reshaping Arrays With Ease

One of the most powerful and commonly used libraries in python is NumPy. It helps us generate high-performance arrays on which we can perform various operations like performing columns wise actions that are not even possible in lists very fast. We can also reshape our arrays without any change in data using one of its built-in functions using NumPy reshape function.

By the shape of an array, we mean the number of elements in each dimension (In 2d array rows and columns are the two dimensions). And by reshaping, we can change the number of dimensions without changing the data.

numpy reshape

Syntax of NumPy Reshape

numpy.reshape(a, new shape, order=’C’)

Parameters in NumPy reshape

  • a: It is the array that we want to reshape.
  • New shape: It is the shape that we want to reshape our old array into. It can be in the form of a single int or tuple containing integers. We should keep in mind is that the new shape given should be compatible with the old shape. You cannot change the 2×3 array into a 1×7
  • For example, if you have an array of size – (2,3) =6 elements, you can only change it to a new array, which can contain six elements like 1×6, 3×2, 6×1.
  • order: {‘C’,’A’,’F’ ; optional} Using this parameter we can read elements of an array in a particular order(index).
    • ‘C’ means that operating row-wise on the array will be slightly faster.
    • ‘F’ means Fortran style, and column-wise operation will be faster.
    • ‘A’ means Fortran-like order if memory management same as ‘F’, C style otherwise.

Converting the array from 1d to 2d using NumPy reshape

1D array means that we have only one column, and n number of rows can be there. We can retrieve any value from the 1d array only by using one attribute – row.
For example, [1,2,3,4,5,6] is a 1d array
A 2d array means that we have any number of rows and any number of columns. Yes, you read it right, any number of columns. If you use three columns, the array will remain a 2d array. In 2d array, we can retrieve any value using two attributes – row and column number.
Now let us learn how to convert a 1d array into a 2d array.
Note- For using numpy you need to install it using – pip install numpy

#This is a list
list1=[1,2,3,4,5,6,7,8]
#Converting the list into array
array=np.array(list1)
#reshaping the array into array with 2 rows and 4 columns
new_array=np.reshape(array,(2,4))
print("shape of original array: ",array.shape)
print("original array: ", array)
print("reshaped array: ", new_array)
print("shape of reshaped array: ",new_array.shape)
Output-
shape of original array: (8,)
original array: [1 2 3 4 5 6 7 8]
reshaped array: [[1 2 3 4]
[5 6 7 8]]
shape of reshaped array: (2, 4)

Some common Errors

If you try to reshape the array into something like (2,3) it would show the following error-
ValueError: cannot reshape array of size 8 into shape (2,3)
It is because of the number of elements in the original array is 8. And we are trying to reshape it into an array with size – (2,3) means 6 elements which are not possible. So please try to keep this thing in mind.

Converting 2D array into a 1D array using NumPy Reshape

Now let us directly convert a 2d array into 1d array. For this we can give ‘-1’ as the argument for new shape parameter. We can convert any 2d array to 1d array using this parameter.

list1=[[11,12,13,14],[15,16,17,18]]
array=np.array(list1)
new_array=np.reshape(array,(-1,1))
print("shape of original array: ",array.shape)
print("original array: ", array)
print("reshaped array: ", new_array)
print("shape of reshaped array: ",new_array.shape)
Output-
shape of original array: (2, 4)
original array: [[11 12 13 14]
[15 16 17 18]]
reshaped array: [[11]
[12]
[13]
[14]
[15]
[16]
[17]
[18]]
shape of reshaped array: (8, 1)


We will get the same result if we do this,

list1=[[11,12,13,14],[15,16,17,18]]
array=np.array(list1)
new_array=np.reshape(array,8)
print("reshaped array: ", new_array)
print("shape of reshaped array: ",new_array.shape)
Output-
reshaped array: [[11] [12] [13] [14] [15] [16] [17] [18]]
shape of reshaped array: (8, 1)

Converting 2d array into 3d array using NumPy Reshape

In a 3d array we have 3 dimensions. To retrieve any value we need three attributes.

list1=[[11,12,13,14],[15,16,17,18]]
array=np.array(list1)
new_array=np.reshape(array,(2,2,2))
print("shape of original array: ",array.shape)
print("original array: ", array)
print("reshaped array: ", new_array)
print("shape of reshaped array: ",new_array.shape)
Output-
shape of original array: (2, 4)
original array: [[11 12 13 14]
[15 16 17 18]]
reshaped array: [[[11 12]
[13 14]]
[[15 16]
[17 18]]]
shape of reshaped array: (2, 2, 2)

We were able to convert a 2d array of size – (2,4)(2*4 = 8 elements) into a 3d array of size (2,2,2) because 22*2=8.

For the above 3d array if we want to retrieve the first element, we can do this-

print(new_array[0][0][0])
Output-
11

Changing the Order parameter in reshape function


Now let us play with the order parameter and observe the changes by changing the ‘order’ parameter to different values.

Output-
reshaped array1: [[1 2 3 4] [5 6 7 8]]
reshaped array2: [[1 3 5 7] [2 4 6 8]]
reshaped array3: [[1 2 3 4] [5 6 7 8]]

We can clearly observe the differences between ‘C’ and ‘F’ orders. In ‘F’ first element into 1st row, the next element in 2nd row, and so on.
Now, let us see what will be the effect of these orders if we convert a 2d array into a 1 d array.

list1=[[11,12,13,14],[15,16,17,18]]
array=np.array(list1)
new_array1=np.reshape(array,(-1,1),order='C')
new_array2=np.reshape(array,(-1,1),order='F')
new_array3=np.reshape(array,(-1,1),order='A')
Order: 'C'
print("reshaped array1: ", new_array1)
Order: 'F'
print("reshaped array2: ", new_array2)
Order: 'A'
print("reshaped array3: ", new_array3)


Again, we can observe that in ‘F,’ the First element of the 1st row and then 1st element of the 2nd row, then the Second element of the 1st row, and then the second element of the 2nd row.

Reshape Numpy Array to 1D

Numpy arrays are a great way of handling your large sets of data. Many times, these arrays are segregated into nested arrays to keeps things simple. But many times, there comes a time when you need to change the dimension of the array into 1D to compute the process on it. For example in Image Recognition, the 2d image array is flattened into a 1D array before processing it. In such cases, you can use the numpy reshape function to convert the array into a 1D array.

Code –

import numpy as np

arr = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
print(arr.reshape(arr.size,))

Output –

[1 2 3 4 5 6 7 8]

Explanation –

We first started by importing the numpy library as an alias “np”. Then we created a dummy 2D array in the second line of the program. In the next line, we used the reshape method to reshape the array with a size array as a parameter. This method converts the existing array into a flattened 1D array.

Must Read

Conclusion

Numpy reshape is a convenient function. Mostly in machine learning and deep learning, different algorithms take arrays in different shapes so we could use this function to reshape the array in any shape required.

Try to run the programs on your side and let us know if you have any queries.

Happy Coding!

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments