Bird
0
0

You have a 3D numpy array arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]]). What is the shape of the result when you run np.sum(arr, axis=1)?

hard📝 Application Q15 of 15
NumPy - Aggregation Functions
You have a 3D numpy array arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]]). What is the shape of the result when you run np.sum(arr, axis=1)?
A(1, 2)
B(3, 2)
C(2, 4)
D(2, 2)
Step-by-Step Solution
Solution:
  1. Step 1: Understand the shape of arr

    arr has shape (2, 2, 2): 2 blocks, each with 2 rows and 2 columns.
  2. Step 2: Summing along axis=1

    axis=1 sums over the second dimension (rows), reducing it from 2 to 1, so shape changes from (2, 2, 2) to (2, 2).
  3. Final Answer:

    (2, 2) -> Option D
  4. Quick Check:

    Sum axis=1 reduces dimension 2 to 1 [OK]
Quick Trick: Sum along axis removes that dimension [OK]
Common Mistakes:
  • Confusing which axis is summed
  • Expecting shape to stay same
  • Mixing up dimensions in 3D arrays

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes