Bird
0
0

What will be the shape of the array after executing the following code?

medium📝 Predict Output Q5 of 15
NumPy - Array Manipulation
What will be the shape of the array after executing the following code?
import numpy as np
arr = np.array([[[5], [6], [7]]])
squeezed_arr = np.squeeze(arr)
A(1, 3, 1)
B(1, 3)
C(3, 1)
D(3,)
Step-by-Step Solution
Solution:
  1. Step 1: Initial shape

    The array arr has shape (1, 3, 1).
  2. Step 2: Applying np.squeeze()

    Removes all axes of length 1, so axes 0 and 2 are removed.
  3. Step 3: Resulting shape

    After squeezing, the shape becomes (3,).
  4. Final Answer:

    (3,) -> Option D
  5. Quick Check:

    All singleton dimensions removed [OK]
Quick Trick: np.squeeze removes all dimensions of size 1 [OK]
Common Mistakes:
  • Forgetting that squeeze removes all singleton dimensions
  • Assuming only one axis is removed
  • Confusing shape after squeeze with original shape

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes