Bird
0
0

Given a 3D NumPy array arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]]), which code correctly computes the product of elements along the last axis (axis=2)?

hard📝 Application Q15 of 15
NumPy - Aggregation Functions
Given a 3D NumPy array arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]]), which code correctly computes the product of elements along the last axis (axis=2)?
Anp.prod(arr, axis=1)
Bnp.prod(arr, axis=2)
Carr.prod(axis=0)
Dnp.prod(arr)
Step-by-Step Solution
Solution:
  1. Step 1: Identify the last axis in 3D array

    For shape (2,2,2), axis=2 is the last axis (elements inside the smallest arrays).
  2. Step 2: Use np.prod with axis=2

    Applying np.prod(arr, axis=2) multiplies elements inside each 1D sub-array.
  3. Final Answer:

    np.prod(arr, axis=2) -> Option B
  4. Quick Check:

    Last axis product = np.prod(arr, axis=2) [OK]
Quick Trick: Last axis in 3D is axis=2, use np.prod(arr, axis=2) [OK]
Common Mistakes:
  • Using wrong axis for product
  • Using arr.prod(axis=0) which multiplies differently
  • Using np.prod(arr) which multiplies all elements

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes