Bird
0
0

What is the shape of the array after executing the following code?

medium📝 Predict Output Q4 of 15
NumPy - Array Manipulation
What is the shape of the array after executing the following code?
import numpy as np
arr = np.array([1, 2, 3])
new_arr = np.expand_dims(arr, axis=1)
A(1, 3)
B(3, 1)
C(3,)
D(1,)
Step-by-Step Solution
Solution:
  1. Step 1: Check original array shape

    arr is 1D with shape (3,).
  2. Step 2: Apply expand_dims at axis=1

    Adding axis at position 1 changes shape to (3, 1).
  3. Final Answer:

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

    expand_dims axis=1 adds axis after first dim [OK]
Quick Trick: Expand dims inserts axis at given position [OK]
Common Mistakes:
  • Confusing axis position
  • Assuming shape stays same
  • Mixing up rows and columns

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes