Bird
0
0

You have a 3D array arr with shape (2, 3, 4). How would you sum all elements along the second axis (axis=1) and what will be the shape of the result?

hard📝 Application Q8 of 15
NumPy - Aggregation Functions
You have a 3D array arr with shape (2, 3, 4). How would you sum all elements along the second axis (axis=1) and what will be the shape of the result?
AUse <code>np.sum(arr, axis=1)</code>, result shape will be (3, 4).
BUse <code>np.sum(arr, axis=1)</code>, result shape will be (2, 4).
CUse <code>np.sum(arr, axis=2)</code>, result shape will be (2, 3).
DUse <code>np.sum(arr, axis=0)</code>, result shape will be (3, 4).
Step-by-Step Solution
Solution:
  1. Step 1: Understand axis=1 in 3D array

    Axis 1 corresponds to the second dimension (size 3).
  2. Step 2: Sum along axis=1

    Summing along axis=1 collapses that dimension, resulting in shape (2, 4).
  3. Final Answer:

    Use np.sum(arr, axis=1), result shape will be (2, 4). -> Option B
  4. Quick Check:

    Sum axis=1 removes second dimension [OK]
Quick Trick: Sum axis removes that dimension from shape [OK]
Common Mistakes:
  • Confusing axis numbers
  • Expecting original shape
  • Using wrong axis for sum

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes