NumPy Reshaping Arrays (Python Tutorial)

NumPy Reshaping Arrays

This is a detailed tutorial of the NumPy Reshaping Arrays. Learn to change the shape of a NumPy Array with the help of illustrative examples.

In the previous topic, we went through the shape of the array and also how we will be able to define them. We also learnt how we could determine the shape of the array by giving various dimensions.

In this section, we will go through the reshaping of the NumPy arrays. What we mean by reshaping an array is that we give shape to the array as per our needs.

We know that shape of the array is the number of elements in any dimension. So when we are able to determine the no of dimension, we want to have in an array that is what we do reshaping. So by reshaping, we mean adding or subtracting dimensions from an array. And also we can change the number of elements present in every dimension.

Let us go through some example to have a better understanding of this concept:

Example 1

In our first example, we are going to convert a NumPy array with elements that are in 1-D into a 2-D array.

#importing the numpy package and also making an alias as np
import numpy as np
# creating the array in 1-D
array1=np.array([9,8,7,6,5,4,3,2,1])
# now we will reshape it
a=array1.reshape(3,3)
#printing the reshaped array
print(a)

Output.

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

Here in this example, we gave a one-dimensional array with nine elements in it. Then we try to reshape it by making it two-dimensional and also by dividing it into three arrays. And also that each array has three elements in each. So, as a result, we get a two-dimensional array with three arrays having three elements in each array.

Example 2

Let us take another example in which we will take a different number of elements:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 1-D 
array1=np.array([9,8,7,6,5,4,3,2,1,9,5,3,2,4,3,2]) 
# now we will reshape it 
a=array1.reshape(4,4) 
#printing the reshaped array 
print(a)

Output.

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

Here in this, we are still converting a one-dimensional array into two-dimensional. But in this example, we are changing the number of the element as in the previous example; we took less number of elements. In this, we are changing it into four arrays with four elements in each array. And it is a two-dimensional array now.

Example 3

In this example, we will be changing the dimension in order to reshape. Here we will be converting a 1-D array into a 3-D array.

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 1-D 
array1=np.array([9,8,7,6,5,4,3,2,1,0,1,2]) 
# now we will reshape it 
a=array1.reshape(2,3,2)
#printing the reshaped array 
print(a)

Output.

[[[9 8]
  [7 6]
  [5 4]]

 [[3 2]
  [1 0]
  [1 2]]]

Here in this example, we are trying to convert the 1-D array into a 3-D array. And we are trying to give it in such form that it has two arrays that contain three arrays, each with two elements in it.

We can reshape an array into any shape that we need as long as the elements we are trying to shape has an equal number of elements in both the shapes. As we can reshape an array with ten elements into two array with five elements in each but we can not reshape into three arrays because for that we need only nine elements. So we have one more element in this case, which would bring errors.

So reshaping of elements highly depend on the number of elements present in the primary array.

Copy or View

We can also check if it is a copy or view while we are trying to reshape an array by using a base attribute.

Let us take an example:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 1-D 
array1=np.array([9,8,7,6,5,4,3,2,1]) 
# now we will reshape it 
a=array1.reshape(3,3) 
#printing the reshaped array 
print(a)
print('if view or copy?',a.base)

Output.

[[9 8 7]
 [6 5 4]
 [3 2 1]]
if view or copy? [9 8 7 6 5 4 3 2 1]

Here it is returning the original array the one we passed, so it is a view.

Unknown Dimensions

In NumPy, we can have one unknown dimension. So in place of this unknown dimension, we will pass -1 as the value. So we do not have to define a particular value for this dimension, but it would be given by NumPy.

Let us take an example to understand it better:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 1-D 
array1=np.array([9,8,7,6,5,4,3,2,1]) 
# now we will reshape it without giving one value
a=array1.reshape(3,-1) 
#printing the reshaped array 
print(a)

Output.

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

Here we have not given second value while reshaping in place of that we have given -1 so it would calculate by itself.

Another example with a 3-D array:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 1-D 
array1=np.array([9,8,7,6,5,4,3,2,1,0,1,2]) 
# now we will reshape it without giving one value
a=array1.reshape(2,-1,2) 
#printing the reshaped array 
print(a)

Output.

[[[9 8]
  [7 6]
  [5 4]]

 [[3 2]
  [1 0]
  [1 2]]]

Here we have not given the value on between the two. So it was calculated by itself.

Flattening NumPy Arrays

FLatttening a NumPy array means changing the dimensions of a multi-dimensional array into a one-dimensional array. We can do this by using this statement in our code which is reshape(-1).

With the help of this, we can convert any dimension into a one-dimension with which we can simplify our arrays.

Let us go through an example:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 2-D 
array1=np.array([[9,8,7,6,5,4],[3,2,1,0,1,2]]) 
# now we will flatten the array 
a=array1.reshape(-1) 
#printing the reshaped array 
print(a)

Output.

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

Here we flatten a two-dimensional array.

Let us take another example for a 3-D array:

#importing the numpy package and also making an alias as np 
import numpy as np 
# creating the array in 3-D 
array1=np.array([[[9,8,7],[6,5,4]],[[3,2,1],[0,1,2]]]) 
# now we will flatten the array 
a=array1.reshape(-1) 
#printing the reshaped array 
print(a)

Output.

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

So we notice that even after reshaping an array, we can convert it back to the original shape. We also have many other functions which can serve this purpose like Flatten and Ravel, but they can be a bit complex.

I hope you found this guide useful. If so, do share it with others who are willing to learn Numpy and Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *