Bird
0
0

Given a 3D array arr with shape (1, 4, 1), you want to add a new axis at the end and then remove all size 1 axes. Which sequence of operations achieves this?

hard📝 Application Q9 of 15
NumPy - Array Manipulation
Given a 3D array arr with shape (1, 4, 1), you want to add a new axis at the end and then remove all size 1 axes. Which sequence of operations achieves this?
Aarr = np.squeeze(arr); arr = np.expand_dims(arr, axis=-1)
Barr = np.expand_dims(arr, axis=-1); arr = np.squeeze(arr)
Carr = np.expand_dims(arr, axis=0); arr = np.squeeze(arr)
Darr = np.squeeze(arr, axis=0); arr = np.expand_dims(arr, axis=1)
Step-by-Step Solution
Solution:
  1. Step 1: Add new axis at the end

    np.expand_dims(arr, axis=-1) adds axis at last position, shape becomes (1, 4, 1, 1).
  2. Step 2: Remove all size 1 axes

    np.squeeze() removes all axes with size 1, resulting in shape (4,).
  3. Final Answer:

    arr = np.expand_dims(arr, axis=-1); arr = np.squeeze(arr) -> Option B
  4. Quick Check:

    Expand dims then squeeze removes all size 1 axes [OK]
Quick Trick: Expand dims adds axis; squeeze removes all size 1 axes [OK]
Common Mistakes:
  • Reversing operation order
  • Squeezing before adding axis
  • Using wrong axis values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes