Bird
0
0

Why does np.cumsum() return a flattened array when called without an axis on a multi-dimensional array?

hard📝 Conceptual Q10 of 15
NumPy - Aggregation Functions
Why does np.cumsum() return a flattened array when called without an axis on a multi-dimensional array?
ABecause it sums only the first dimension by default
BBecause it treats the array as 1D by flattening before summing
CBecause it raises an error and returns a flattened copy
DBecause it sums elements column-wise automatically
Step-by-Step Solution
Solution:
  1. Step 1: Check default axis behavior

    When axis is None, numpy flattens the array to 1D before operation.
  2. Step 2: Understand why flattening happens

    This ensures cumulative sum is computed over all elements in order, ignoring original shape.
  3. Final Answer:

    Because it treats the array as 1D by flattening before summing -> Option B
  4. Quick Check:

    Default axis=None means flatten array first [OK]
Quick Trick: No axis means flatten array before cumsum [OK]
Common Mistakes:
  • Thinking it sums only first dimension
  • Expecting error without axis
  • Assuming column-wise sum by default

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More NumPy Quizzes