Complete the code to add a new axis to the 1D array to make it 2D.
import numpy as np arr = np.array([1, 2, 3]) new_arr = arr[1] print(new_arr.shape)
[np.newaxis] alone does not add a new axis correctly.np.newaxis in the wrong position changes the shape differently.Using [:, np.newaxis] adds a new axis as a column, changing shape from (3,) to (3, 1).
Complete the code to add a new axis at the start of the array shape.
import numpy as np arr = np.array([4, 5, 6]) new_arr = arr[1] print(new_arr.shape)
np.newaxis after the colon adds the axis at the end, not the start.[np.newaxis] does not specify the position clearly.Using [np.newaxis, :] adds a new axis at the start, changing shape from (3,) to (1, 3).
Fix the error in the code to correctly add a new axis to the array.
import numpy as np arr = np.array([7, 8, 9]) new_arr = arr[1] print(new_arr.shape)
np.newaxis without slicing causes errors.np.newaxis inside brackets incorrectly changes the shape.The correct way to add a new axis as a column is [:, np.newaxis]. Using np.newaxis alone or in wrong positions causes errors or wrong shapes.
Fill both blanks to create a 3D array by adding two new axes to the 1D array.
import numpy as np arr = np.array([10, 20, 30]) new_arr = arr[1][2] print(new_arr.shape)
[np.newaxis, np.newaxis] once instead of chaining twice.Adding [np.newaxis] twice adds two new axes, changing shape from (3,) to (1, 1, 3).
Fill all three blanks to create a 4D array with shape (1, 1, 1, 3) from a 1D array.
import numpy as np arr = np.array([100, 200, 300]) new_arr = arr[1][2][3] print(new_arr.shape)
Using [np.newaxis, np.newaxis] adds two new axes at the start, then [np.newaxis] adds another, and [:, np.newaxis] adds a new axis as column, resulting in shape (1, 1, 1, 3).