Bird
0
0

What will be the output of this code?

medium📝 Predict Output Q5 of 15
NumPy - Aggregation Functions
What will be the output of this code?
import numpy as np
arr = np.array([[1, 2], [3, 4]])
cum_sum = np.cumsum(arr, axis=1)
print(cum_sum)
A[[1 2] [3 4]]
B[[1 3] [4 7]]
C[[1 2] [4 6]]
D[[1 3] [3 7]]
Step-by-Step Solution
Solution:
  1. Step 1: Understand axis=1 cumulative sum

    Axis 1 means sum across columns (row-wise).
  2. Step 2: Calculate cumulative sums for each row

    Row 1: 1, 1+2=3; Row 2: 3, 3+4=7.
  3. Final Answer:

    [[1 3] [3 7]] -> Option D
  4. Quick Check:

    Axis=1 cumulative sum sums row-wise [OK]
Quick Trick: Axis=1 sums across rows, left to right [OK]
Common Mistakes:
  • Summing column-wise instead
  • Confusing axis parameter
  • Expecting flattened output

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes