Bird
Raised Fist0
NumPydata~20 mins

np.expand_dims() and np.squeeze() in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Numpy Dimension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of np.expand_dims() on a 1D array
What is the output shape of the array after applying np.expand_dims() on a 1D array along axis 0?
NumPy
import numpy as np
arr = np.array([1, 2, 3])
expanded = np.expand_dims(arr, axis=0)
print(expanded.shape)
A(3,)
B(3, 1)
C(1, 3)
D(1,)
Attempts:
2 left
💡 Hint
Think about adding a new dimension at the start of the shape.
❓ Predict Output
intermediate
2:00remaining
Effect of np.squeeze() on a 3D array
Given a 3D array with shape (1, 4, 1), what is the shape after applying np.squeeze()?
NumPy
import numpy as np
arr = np.zeros((1, 4, 1))
squeezed = np.squeeze(arr)
print(squeezed.shape)
A(1, 4, 1)
B(4,)
C(4, 1)
D(1, 4)
Attempts:
2 left
💡 Hint
np.squeeze removes all dimensions of size 1.
🔧 Debug
advanced
2:00remaining
Identify the error in np.expand_dims usage
Which option will cause an error when trying to expand dimensions of a 2D array with shape (3, 4) using np.expand_dims()?
NumPy
import numpy as np
arr = np.ones((3, 4))
expanded = np.expand_dims(arr, axis=3)
print(expanded.shape)
Aaxis=4
Baxis=3
Caxis=-1
Daxis=2
Attempts:
2 left
💡 Hint
Check if the axis value is valid for the array's dimensions.
❓ data_output
advanced
2:00remaining
Result of squeezing with specific axis
What is the shape of the array after applying np.squeeze() with axis=0 on an array of shape (1, 5, 1)?
NumPy
import numpy as np
arr = np.zeros((1, 5, 1))
squeezed = np.squeeze(arr, axis=0)
print(squeezed.shape)
A(5, 1)
B(1, 5)
C(5,)
D(1, 5, 1)
Attempts:
2 left
💡 Hint
Only the specified axis with size 1 is removed.
🚀 Application
expert
3:00remaining
Using np.expand_dims and np.squeeze to reshape data
You have a 1D array of shape (10,). You want to reshape it to (10, 1) for a machine learning model input, then later revert it back to (10,) after prediction. Which sequence of operations correctly achieves this?
NumPy
import numpy as np
arr = np.arange(10)
# reshape for model input
arr_expanded = np.expand_dims(arr, axis=1)
# ... model prediction happens ...
# revert to original shape
arr_reverted = np.squeeze(arr_expanded, axis=1)
print(arr_reverted.shape)
AUse np.expand_dims(arr, axis=0) then np.squeeze(arr_expanded)
BUse np.expand_dims(arr, axis=0) then np.squeeze(arr_expanded, axis=0)
CUse np.expand_dims(arr, axis=1) then np.squeeze(arr_expanded)
DUse np.expand_dims(arr, axis=1) then np.squeeze(arr_expanded, axis=1)
Attempts:
2 left
💡 Hint
Match the axis used in expand_dims and squeeze to correctly revert shape.

Practice

(1/5)
1. What does np.expand_dims() do to a NumPy array?
easy
A. Adds a new dimension at the specified axis
B. Removes all dimensions of size one
C. Flattens the array into one dimension
D. Changes the data type of the array

Solution

  1. Step 1: Understand the function purpose

    np.expand_dims() is used to add a new axis to an array, increasing its dimensions.
  2. Step 2: Compare with other options

    Removing dimensions or flattening is done by other functions like np.squeeze() or reshape(). Changing data type is unrelated.
  3. Final Answer:

    Adds a new dimension at the specified axis -> Option A
  4. Quick Check:

    np.expand_dims() = Adds dimension [OK]
Hint: Expand dims adds axis, squeeze removes axis [OK]
Common Mistakes:
  • Confusing expand_dims with squeeze
  • Thinking it changes data values
  • Assuming it flattens the array
2. Which of the following is the correct syntax to add a new axis at position 1 using np.expand_dims() on array a?
easy
A. np.expand_dims(a, axis=0)
B. np.expand_dims(a, 0)
C. np.expand_dims(a, axis=1)
D. np.expand_dims(a, axis=2)

Solution

  1. Step 1: Check the function signature

    np.expand_dims(array, axis) requires the axis parameter to specify where to add the new dimension.
  2. Step 2: Identify correct axis value

    Axis=1 adds the new dimension at position 1. np.expand_dims(a, 0) (positional) and np.expand_dims(a, axis=0) add the dimension at position 0. np.expand_dims(a, axis=2) causes an AxisError since valid axes are -2 to 1 for a 1D array.
  3. Final Answer:

    np.expand_dims(a, axis=1) -> Option C
  4. Quick Check:

    Use axis=1 to add dimension at position 1 [OK]
Hint: Use axis= to specify position clearly [OK]
Common Mistakes:
  • Omitting axis keyword and confusing position
  • Using wrong axis number
  • Mixing axis=0 and axis=1
3. What is the shape of the array after applying np.squeeze() to arr = np.array([[[1], [2], [3]]])?
medium
A. (3,)
B. (1, 3, 1)
C. (3, 1)
D. (3, 1, 1)

Solution

  1. Step 1: Check original shape

    arr has shape (1, 3, 1) because it is a 3D array with one element in first and last dimension.
  2. Step 2: Apply np.squeeze()

    np.squeeze() removes all dimensions of size 1, so the first and last dimensions are removed, leaving shape (3,).
  3. Final Answer:

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

    squeeze removes size 1 dims = (3,) [OK]
Hint: Squeeze removes all size 1 dims from shape [OK]
Common Mistakes:
  • Assuming squeeze removes only one dimension
  • Confusing original shape with squeezed shape
  • Thinking squeeze flattens the array
4. Given arr = np.array([1, 2, 3]), which code will cause an error?
medium
A. np.expand_dims(arr, axis=-1)
B. np.expand_dims(arr, axis=0)
C. np.squeeze(arr)
D. np.expand_dims(arr, axis=3)

Solution

  1. Step 1: Check original array shape

    arr has shape (3,), so it has 1 dimension.
  2. Step 2: Validate axis values for expand_dims

    Valid axis values for adding a new dimension are between -2 and 1 (inclusive) because adding a dimension increases rank by 1. Axis=3 is out of bounds and causes an error.
  3. Final Answer:

    np.expand_dims(arr, axis=3) causes error -> Option D
  4. Quick Check:

    Axis out of range causes error [OK]
Hint: Axis must be in valid range for expand_dims [OK]
Common Mistakes:
  • Using axis larger than array ndim + 1
  • Confusing axis indexing with array shape
  • Assuming squeeze can cause error on 1D arrays
5. You have a 2D array data with shape (5, 1). You want to prepare it for a machine learning model that expects input shape (5,). Which code correctly reshapes data using np.squeeze() and np.expand_dims()?
hard
A. np.expand_dims(np.squeeze(data), axis=1)
B. np.squeeze(data)
C. np.squeeze(np.expand_dims(data, axis=-1))
D. np.expand_dims(data, axis=-1)

Solution

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

    data shape is (5, 1). The model expects (5,), so we need to remove the extra dimension.
  2. Step 2: Use np.squeeze() to remove size 1 dimension

    np.squeeze(data) removes the dimension of size 1, resulting in shape (5,). Adding dimensions again is unnecessary and would change shape incorrectly.
  3. Final Answer:

    np.squeeze(data) -> Option B
  4. Quick Check:

    Squeeze removes extra dims to match model input [OK]
Hint: Squeeze to remove size 1 dims for model input [OK]
Common Mistakes:
  • Adding dimensions instead of removing
  • Using expand_dims unnecessarily
  • Not matching model expected input 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