لَآ إِلَـٰهَ إِلَّا هُوَ
LA ILAHA ILLA HU
Allah, Your Lord There Is No Deity Except Him.

# Python Data Science NumPy Array Reshaping

Reshaping arrays: Reshaping means changing the shape of an array.

The shape of an array is the number of elements in each dimension.

By reshaping we can add or remove dimensions or change number of elements in each dimension.

Reshaping An Array From 1-D to 2-D

Example 1: Convert the following 1-D array with 12 elements into a 2-D array. The outermost dimension will have 4 arrays, each with 3 elements.

Code

import numpy as np

arr = np.array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32])

newarr = arr.reshape(4, 3)

print(newarr)

the output will be

[[21 22 23]
[24 25 26]
[27 28 29]
[30 31 32]]

Example 1-B: Convert the following 1-D array with 12 elements into a 2-D array. The outermost dimension will have 3 arrays, each with 4 elements.

Code

import numpy as np

arr = np.array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32])

newarr = arr.reshape(3, 4)

print(newarr)

the output will be

[[21 22 23 24]
[25 26 27 28]
[29 30 31 32]]

Reshaping An Array From 1-D to 3-D

Example 2: Convert the following 1-D array with 12 elements into a 3-D array.

The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements.

Code

import numpy as np

arr = np.array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32])

newarr = arr.reshape(2, 3, 2)

print(newarr)

the output will be

[[[21 22]
[23 24]
[25 26]]

[[27 28]
[29 30]
[31 32]]]

Can We Reshape Into any Shape?

Yes, as long as the elements required for reshaping are equal in both shapes.

We can reshape an 8 elements 1D array into 4 elements in 2 rows 2D array but we cannot reshape it into a 3 elements 3 rows 2D array as that would require 3x3 = 9 elements.

Example 3: Try converting 1D array with 8 elements to a 2D array with 3 elements in each dimension (will raise a Value Error).

Code

import numpy as np

arr = np.array([11, 12, 13, 14, 15, 16, 17, 18])

newarr = arr.reshape(3, 3)

print(newarr)

the output will be

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

How to Check if the returned array is a Copy or View?

Example 4: Check if the returned array is a copy or a view.

Code

import numpy as np

arr = np.array([11, 12, 13, 14, 15, 16, 17, 18])

print(arr.reshape(2, 4).base)

the output will be

[11 12 13 14 15 16 17 18]

Since the example above returns the original array, so it is a view.

Unknown Dimension

You are allowed to have one "unknown" dimension.

It means that you do not have to specify an exact number for one of the dimensions in the reshape method.

Pass -1 as the value, and NumPy will calculate this number for you.

Example 5: Convert 1D array with 8 elements to 3D array with 2x2 elements.

Code

import numpy as np

arr = np.array([11, 12, 13, 14, 15, 16, 17, 18])

newarr = arr.reshape(2, 2, -1)

print(newarr)

the output will be

[[[11 12]
[13 14]]

[[15 16]
[17 18]]]

Note: We can not pass -1 to more than one dimension.

Flattening the arrays

Flattening array means converting a multidimensional array into a 1D array.

We can use reshape(-1) to do this.

Example 6: Convert the array into a 1D array.

Code

import numpy as np

arr = np.array([[11, 12, 13], [14, 15, 16]])

newarr = arr.reshape(-1)

print(newarr)

the output will be

[11 12 13 14 15 16]

Note: There are a lot of functions for changing the shapes of arrays in numpy flatten, ravel and also for rearranging the elements rot90, flip, fliplr, flipud etc.
These fall under Intermediate to Advanced section of numpy.