Halloween Sale: Flat 70% + Addl. 25% Off + 30 Days Extra Lab on all Courses | Use Coupon HS25 in Checkout | Offer Expires In

  Enroll Now

NumPy - Arrays - Reshaping an Array

reshape()

reshape() function is used to create a new array of the same size (as the original array) but of different desired dimensions.

reshape() function will create an array with the same number of elements as the original array, i.e. of the same size as that of the original array. If you want to convert the original array to a bigger array, reshape() can’t add more elements(than available in the original array) to give you a bigger array.

Below is the syntax for reshape() function

ndarray.reshape(new_shape)

There may be two cases, as mentioned below when we are reshaping the array:

(1) We already know the desired number dimensions (say rows and columns) of the reshaped array.

(2) We know the desired number of columns, but don't know the desired number of rows of the reshaped array.


Lets us take the first case - Reshaping a numpy array when the number of rows and number of columns of the reshaped array is known.

  • This technique is used when you already are aware - how many numbers of rows and columns your reshaped array will have.

    my_arr = np.arange(6)
    
    print(my_arr)
    
  • output will be

    [0,1,2,3,4,5]
    
  • Now, let us reshape this array

    my_new_arr = my_arr.reshape(2,3)
    
    print(my_new_arr)
    
  • output will be

    [ [0,1,2],
      [3,4,5] ]
    



Now, let us take the second case - Reshaping a numpy array when the desired number of columns is known, but the desired number of rows is unknown.

  • This specific technique is used when your reshaped array is going to be of bigger size than the original array, and you are sure about how many columns you want in the reshaped array, but you don't have an idea how many rows will be there in the reshaped (output) array.

  • To achieve this, you use a value of '-1' in place of 'rows' field in the reshape() function, and use the desired value of the number of columns you want in the columns field in the reshape() function.

  • Example - Say you want to reshape an existing array to an array with 3 columns and an unknown number of rows, as shown in the code below:

    my_arr = np.arange(9)
    
    print(my_arr)
    
  • output will be

    [0,1,2,3,4,5,6,7,8]
    
  • Now, let us reshape this array

    my_new_arr = my_arr.reshape(-1,3)
    
    print(my_new_arr)
    
  • output will be

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


When you can't use reshape() function?

You can't use reshape() function, when the size of the original array is different from your desired reshaped array. If you try to reshape(), it will throw an error.

  • Example

    my_arr = np.arange(8)
    print(my_arr)
    
  • output will be

    [0,1,2,3,4,5,6,7]
    my_arr.reshape(2,3)
    

    the output will be an error as shown below

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


  • Now, again try with some other shape as shown below:

    my_arr.reshape(3,3)
    
  • the output will be an error as shown below

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


Difference between resize() and reshape() :

reshape() will create an array with the same number of elements as the original array, i.e. of the same ‘size’ as that of the original array. If you want to convert the original array to a bigger array, reshape() can’t add more elements(than available in the original array) to give you a bigger array. Hence, reshape() is used to create a new array of the same size (as the original array) but of different desired dimensions.

resize() can create an array of larger size than the original array. To convert the original array into a bigger array, resize() will add more elements (than available in the original array) by copying the existing elements (repeated as many times as required) to fill the desired larger size. resize() is used to create a new array of different sizes and dimensions.

INSTRUCTIONS

Please follow the below-mentioned steps:


First case: When, for the reshaped array, the number of desired rows and columns are known.

(1) Please import numpy as np

(2) Please create a 1-D numpy array called my_first_arr with values - 1,2,3,4,5,6,7 and 8, using array() function of numpy library

<<your code comes here>> = np.array(<<your code comes here>>)

(3) Please apply reshape() function on this my_first_arr array to convert this 1-D array into a (2x4) array and store this newly created (2x4) array in a variable called my_new_arr

<<your code comes here>> = my_first_arr.reshape(<<your code comes here>>)

(4) Please print this new array (my_new_arr) to see its values

(5) Please print the existing array (my_first_arr) to see its values


Second case: When, for the reshaped array, the number of desired columns is known, but the desired number of rows is not known.

(1) Please import numpy as np

(2) Please create a 1-D numpy array called my_second_arr with values - 0,1,2,3,4,5,6,7 and 8, using array() function of numpy library

<<your code comes here>> = np.array(<<your code comes here>>)

(3) Please apply reshape() function on this my_second_arr array to convert this 1-D array into an array with 3 columns but unknown number of rows, and store this newly created array in a variable called my_updated_arr

<<your code comes here>> = my_second_arr.reshape(<<your code comes here>>)

(4) Please print this new array (my_updated_arr) to see its values

(5) Please print the existing array (my_second_arr) to see its values


No hints are availble for this assesment

Answer is not availble for this assesment


Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...