Bird
0
0

How do you correctly compute the cumulative sum of a 2D numpy array arr along the first axis (rows)?

easy📝 Conceptual Q3 of 15
NumPy - Aggregation Functions
How do you correctly compute the cumulative sum of a 2D numpy array arr along the first axis (rows)?
Anp.cumsum(arr, axis=0)
Bnp.cumsum(arr, axis=1)
Cnp.cumsum(arr)
Dnp.cumsum(arr, axis=2)
Step-by-Step Solution
Solution:
  1. Step 1: Identify axis for rows

    In numpy, axis=0 corresponds to rows.
  2. Step 2: Use np.cumsum with axis=0

    This computes cumulative sums down each column across rows.
  3. Final Answer:

    np.cumsum(arr, axis=0) -> Option A
  4. Quick Check:

    Verify cumulative sums increase down rows, not across columns [OK]
Quick Trick: Axis 0 sums down rows, axis 1 sums across columns [OK]
Common Mistakes:
  • Mixing up axis=0 and axis=1
  • Omitting axis parameter leading to flattened sum
  • Using invalid axis values

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes