Bird
Raised Fist0
NumPydata~10 mins

np.newaxis for adding dimensions in NumPy - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to add a new axis to the 1D array to make it 2D.

NumPy
import numpy as np
arr = np.array([1, 2, 3])
new_arr = arr[1]
print(new_arr.shape)
Drag options to blanks, or click blank then click option'
A[np.newaxis, np.newaxis]
B[np.newaxis, :]
C[np.newaxis]
D[:, np.newaxis]
Attempts:
3 left
💡 Hint
Common Mistakes
Using [np.newaxis] alone does not add a new axis correctly.
Adding np.newaxis in the wrong position changes the shape differently.
2fill in blank
medium

Complete the code to add a new axis at the start of the array shape.

NumPy
import numpy as np
arr = np.array([4, 5, 6])
new_arr = arr[1]
print(new_arr.shape)
Drag options to blanks, or click blank then click option'
A[np.newaxis, :]
B[:, np.newaxis]
C[np.newaxis]
D[np.newaxis, np.newaxis]
Attempts:
3 left
💡 Hint
Common Mistakes
Placing np.newaxis after the colon adds the axis at the end, not the start.
Using just [np.newaxis] does not specify the position clearly.
3fill in blank
hard

Fix the error in the code to correctly add a new axis to the array.

NumPy
import numpy as np
arr = np.array([7, 8, 9])
new_arr = arr[1]
print(new_arr.shape)
Drag options to blanks, or click blank then click option'
A[np.newaxis]
B[np.newaxis, :]
C[:, np.newaxis]
Dnp.newaxis
Attempts:
3 left
💡 Hint
Common Mistakes
Using np.newaxis without slicing causes errors.
Placing np.newaxis inside brackets incorrectly changes the shape.
4fill in blank
hard

Fill both blanks to create a 3D array by adding two new axes to the 1D array.

NumPy
import numpy as np
arr = np.array([10, 20, 30])
new_arr = arr[1][2]
print(new_arr.shape)
Drag options to blanks, or click blank then click option'
A[np.newaxis]
B[:, np.newaxis]
C[np.newaxis, np.newaxis]
D[np.newaxis, :]
Attempts:
3 left
💡 Hint
Common Mistakes
Using combined axes like [np.newaxis, np.newaxis] once instead of chaining twice.
Mixing slicing and brackets incorrectly.
5fill in blank
hard

Fill all three blanks to create a 4D array with shape (1, 1, 1, 3) from a 1D array.

NumPy
import numpy as np
arr = np.array([100, 200, 300])
new_arr = arr[1][2][3]
print(new_arr.shape)
Drag options to blanks, or click blank then click option'
A[np.newaxis]
B[:, np.newaxis]
C[np.newaxis, :]
D[np.newaxis, np.newaxis]
Attempts:
3 left
💡 Hint
Common Mistakes
Adding axes in wrong order changes the shape.
Using combined axes incorrectly.

Practice

(1/5)
1. What does np.newaxis do when used inside array indexing?
easy
A. Changes the data type of the array elements
B. Deletes a dimension from the array
C. Flattens the array into one dimension
D. Adds a new dimension to the array without copying data

Solution

  1. Step 1: Understand the role of np.newaxis

    np.newaxis is used to add a new axis (dimension) to an existing array, increasing its dimensionality by one.
  2. Step 2: Confirm what np.newaxis does not do

    It does not remove dimensions, change data types, or flatten arrays.
  3. Final Answer:

    Adds a new dimension to the array without copying data -> Option D
  4. Quick Check:

    np.newaxis adds dimension = C [OK]
Hint: np.newaxis adds a dimension, never removes or flattens [OK]
Common Mistakes:
  • Thinking np.newaxis removes dimensions
  • Confusing np.newaxis with flattening
  • Assuming it changes data types
2. Which of the following is the correct syntax to add a new axis to a 1D numpy array a to make it a column vector?
easy
A. a[:, np.newaxis]
B. a[np.newaxis, :]
C. a[np.newaxis]
D. a[:, :np.newaxis]

Solution

  1. Step 1: Understand array shape and axis addition

    To convert a 1D array to a column vector, we add a new axis as the second dimension using a[:, np.newaxis].
  2. Step 2: Check each option

    a[np.newaxis, :] adds a new axis at the front, making a row vector (1, 3). a[np.newaxis] does the same, making a row vector (1, 3). a[:, :np.newaxis] runs but does not add a dimension since :np.newaxis is equivalent to :, keeping shape (3,).
  3. Final Answer:

    a[:, np.newaxis] -> Option A
  4. Quick Check:

    Column vector = a[:, np.newaxis] [OK]
Hint: Use a[:, np.newaxis] to add a column dimension [OK]
Common Mistakes:
  • Using np.newaxis in wrong position
  • Confusing row and column vector shapes
  • Syntax errors like a[:, :np.newaxis]
3. Given the code:
import numpy as np
arr = np.array([1, 2, 3])
new_arr = arr[:, np.newaxis]
print(new_arr.shape)

What is the output?
medium
A. (3, 1)
B. (1, 3)
C. (3,)
D. (1,)

Solution

  1. Step 1: Check original array shape

    The original array arr is 1D with shape (3,).
  2. Step 2: Adding new axis with arr[:, np.newaxis]

    This adds a new axis as the second dimension, changing shape to (3, 1).
  3. Final Answer:

    (3, 1) -> Option A
  4. Quick Check:

    Shape after newaxis = (3, 1) [OK]
Hint: Adding np.newaxis in second position adds a new column dimension [OK]
Common Mistakes:
  • Confusing (1, 3) and (3, 1) shapes
  • Forgetting newaxis adds dimension, not removes
  • Assuming shape stays (3,)
4. What is wrong with this code snippet?
import numpy as np
arr = np.array([1, 2, 3])
new_arr = arr[np.newaxis:]
print(new_arr.shape)
medium
A. new_arr will have shape (3, 1)
B. new_arr will be a flattened array
C. Fails to add a new dimension due to incorrect slicing with np.newaxis
D. Code runs fine and adds a new axis

Solution

  1. Step 1: Analyze the slicing syntax

    The code uses arr[np.newaxis:], which does not cause SyntaxError but treats np.newaxis (alias for None) as slice start, equivalent to arr[:], so shape remains (3,).
  2. Step 2: Understand correct usage of np.newaxis

    np.newaxis must be used inside index brackets as a separate index, not as part of a slice.
  3. Final Answer:

    Fails to add a new dimension due to incorrect slicing with np.newaxis -> Option C
  4. Quick Check:

    np.newaxis must be separate index, not slice [OK]
Hint: np.newaxis is an index, not a slice; avoid np.newaxis: [OK]
Common Mistakes:
  • Using np.newaxis as part of slice syntax
  • Expecting code to run without error
  • Confusing np.newaxis with slice objects
5. You have a 2D numpy array data with shape (100, 50). You want to add a new dimension at the front to represent batch size 1, making the shape (1, 100, 50). Which code correctly does this using np.newaxis?
hard
A. data[np.newaxis:]
B. data[np.newaxis, :, :]
C. data[:, :, np.newaxis]
D. data[:, np.newaxis, :]

Solution

  1. Step 1: Understand the original shape and desired shape

    Original shape is (100, 50). We want to add a new axis at the front to get (1, 100, 50).
  2. Step 2: Use np.newaxis at the front index

    Using data[np.newaxis, :, :] adds a new axis at position 0, resulting in shape (1, 100, 50).
  3. Step 3: Check other options

    data[np.newaxis:] does not add a new dimension (acts as slice data[:], shape (100,50)). data[:, np.newaxis, :] adds axis in middle (100, 1, 50), data[:, :, np.newaxis] adds axis at end (100, 50, 1).
  4. Final Answer:

    data[np.newaxis, :, :] -> Option B
  5. Quick Check:

    New axis front = data[np.newaxis, :, :] [OK]
Hint: Add np.newaxis at front index for batch dimension [OK]
Common Mistakes:
  • Adding newaxis in wrong position
  • Using fewer indices than array dimensions
  • Confusing axis order for shape
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard