Bird
0
0

Given a 3D NumPy array arr with shape (3, 4, 5), which code snippet correctly computes the product of elements along the second axis?

hard📝 Application Q8 of 15
NumPy - Aggregation Functions
Given a 3D NumPy array arr with shape (3, 4, 5), which code snippet correctly computes the product of elements along the second axis?
Anp.prod(arr, axis=0)
Bnp.prod(arr, axis=2)
Cnp.prod(arr, axis=1)
Dnp.prod(arr)
Step-by-Step Solution
Solution:
  1. Step 1: Understand axes in 3D arrays

    For shape (3, 4, 5), axis=0 has size 3, axis=1 has size 4, axis=2 has size 5.
  2. Step 2: Identify the second axis

    The second axis corresponds to axis=1.
  3. Step 3: Use np.prod with axis=1

    To compute product along the second axis, use np.prod(arr, axis=1).
  4. Final Answer:

    np.prod(arr, axis=1) -> Option C
  5. Quick Check:

    Axis numbering starts at 0 [OK]
Quick Trick: Axis 1 is the second axis in any NumPy array [OK]
Common Mistakes:
  • Confusing axis numbering starting at 0
  • Using axis=2 instead of axis=1 for second axis

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes