Machine Learning Prerequisites (Numpy)

16 / 32

NumPy - Arrays - Resizing an Array

Resize()

resize() function is used to create a new array of different sizes and 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.

Below is the syntax of resize() function:

ndarray.resize(new_shape, refcheck=True)

With the above resize() function, enlarging an array to a larger size will retain the existing values of the original array, but missing entries/values will be filled with zeros.


Example code

import numpy as np

my_arr = np.array([ [0,1], [2,3] ])

my_arr.resize( (2, 3) )

print(my_arr)

output will be:

array[ [0, 1, 2], 
           [3, 0, 0] ]
INSTRUCTIONS

Please follow the below-mentioned steps:

(1) Please import numpy as np

(2) Please create a (2X4) numpy array (i.e. with 2 rows and 4 columns) called my_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 resize() function on this my_arr array to convert this (2X4) array into a (3x4) array.

my_arr.resize(<<your code comes here>>)

(4) Please print this array (my_arr) to see its values

See Answer

No hints are availble for this assesment


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

Loading comments...