Skip to main content

NumPy Array Reshaping

Screenshot
Body

In this tutorial you will learn:

  • NumPy Array Shape
  • Reshape 1D Array to 2D Array
  • Reshape 2D Array to 3D Array

NumPy Array Shape

After necessary slicing or concatenation of the array we sometimes have to confirm the size and dimensions of the array in order to make sure that the data format and size meets the desired requirements of specific API, for example some of the algorithms in Keras only accept the 3D array only, so before input of data we will ensure that the data conforms to the required format and size. NumPy array have a built in attribute shape() which returns the length of each dimension of the array in a tuple.

In this example we will declare a 3 dimensional array having 5 indexes and in the end we display the shape of the array.

  1. import numpy as np
  2. my_arr = np.array([15,20,25,30,35], ndmin=3)
  3. print('My 3 dimensional array is:',my_arr)
  4. print('The shape of 3 dimensional array is:',my_arr.shape)

In this example we declare a 2D and display its shape. The shape of an array is further used to display rows and columns of 2D array.

  1. import numpy as np
  2. my_arr = np.array([[15,20,25,30,35], [12,24,36,48,60]])
  3. print('My 2 dimensional array is:',my_arr)
  4. print('The shape of 2 dimensional array is:',my_arr.shape)
  5. print('The rows in the array are:',my_arr.shape[0])
  6. print('The columns in the array are:',my_arr.shape[1])

Reshape 1D array to 2D array

Inorder to meet specific input requirements, at times we need to address the issue of reshaping an array. To serve the purpose, NumPy provides a function reshape() which takes in 2 arguments, first argument tells if we are reshaping the row or the column while the second argument indicates the change in dimension.

In this example I will demonstrate the reshaping of 1D array into a 2D array.

  1. import numpy as np
  2. my_arr = np.array([1, 2, 3, 4])
  3. print('The shape of a 1D array before reshaping it:', my_arr.shape)
  4. my_arr = my_arr.reshape(my_arr.shape[0],1)
  5. print('The shape of a 2D array after reshaping it:',my_arr.shape)

In this example, we converted a 8 indexed 1 D array to 4 indexed 2 D array by using reshape() function.

  1. import numpy as np
  2. my_arr = np.array([1,2,3,4,5,6,7,8])
  3. print('The shape of a 1D array before reshaping it:', my_arr.shape)
  4. my_arr = my_arr.reshape(4,2)
  5. print('The shape of a 2D array after reshaping it:',my_arr.shape)

It is necessary that the no. of elements inside the array being reshaped directly correspond to the size and dimensions of the new array. The example below will generate an error as we have 1 D array of 5 indexes only and to make a 2 D array we will need either 4(2x2) or 6(2x3) indexes.

  1. import numpy as np
  2. my_arr = np.array([1,2,3,4,5])
  3. print('The shape of a 1D array before reshaping it:', my_arr.shape)
  4. my_arr = my_arr.reshape(2,2)
  5. print('The shape of a 2D array after reshaping it:',my_arr.shape)

Reshape 2D array to 3D array

Reshaping 2D array to 3D array is usually necessary in order to meet the input requirements of a specific algorithm, however the same is achievable through reshape() func as the same was used to reshape 1D array into 2D array. In this example we have reshaped an array from 2D to 3D and also visualized it.

  1. import numpy as np
  2. my_arr=np.array([[11, 22],
  3. [33, 44],
  4. [55, 66]])
  5. print('The visualization of a 2D array before reshaping it:',my_arr)
  6. print('The shape of a 2D array before reshaping it:', my_arr.shape)
  7. my_arr = my_arr.reshape((my_arr.shape[0], my_arr.shape[1], 1))
  8. print('The shape of a 3D array after reshaping it:',my_arr.shape)
  9. print('The visualization of a 3D array after reshaping it:', my_arr)

Tags

Add new comment