Bird
Raised Fist0
NumPydata~5 mins

np.sum() and axis parameter in NumPy - Cheat Sheet & Quick Revision

Choose your learning style10 modes available
Recall & Review
beginner
What does the np.sum() function do in NumPy?

np.sum() adds up all the numbers in an array. It can add all elements or add along a specific direction (axis).

Click to reveal answer
beginner
What is the purpose of the axis parameter in np.sum()?

The axis parameter tells np.sum() which direction to add numbers along. For example, axis=0 adds down columns, axis=1 adds across rows.

Click to reveal answer
beginner
If you have a 2D array, what does np.sum(array, axis=0) do?

It adds the numbers down each column and returns a 1D array with sums for each column.

Click to reveal answer
beginner
What happens if you call np.sum() without specifying the axis?

It adds all the numbers in the whole array and returns a single number.

Click to reveal answer
beginner
Explain axis=1 in np.sum() for a 2D array.

axis=1 means add numbers across each row. The result is a 1D array with the sum of each row.

Click to reveal answer
What does np.sum(array, axis=0) do for a 2D array?
AAdds numbers across each row
BAdds numbers down each column
CAdds all numbers in the array
DSubtracts numbers in each column
If you want to add all elements in a NumPy array, what should you do?
AUse <code>np.sum(array, axis=0)</code>
BUse <code>np.sum(array, axis=1)</code>
CUse <code>np.mean(array)</code>
DUse <code>np.sum(array)</code> without axis
What does axis=1 mean in np.sum() for a 2D array?
AAdd across rows
BAdd down columns
CAdd all elements
DMultiply elements
What type of result does np.sum() return when you specify an axis?
AA list of original numbers
BA single number
CAn array with sums along that axis
DA boolean value
Which axis sums down the columns in a 2D NumPy array?
Aaxis=0
Baxis=1
Caxis=2
Daxis=-1
Describe how the axis parameter changes the behavior of np.sum() when working with 2D arrays.
Think about adding numbers in rows vs columns.
You got /4 concepts.
    Explain what happens when you use np.sum() on a 3D array with axis=1.
    Imagine stacking multiple 2D arrays and summing across the middle dimension.
    You got /4 concepts.

      Practice

      (1/5)
      1. What does the axis parameter in np.sum() control?
      easy
      A. The data type of the output array.
      B. The direction along which the sum is calculated (rows or columns).
      C. The size of the array to sum.
      D. The number of times the sum is repeated.

      Solution

      1. Step 1: Understand the role of axis in np.sum()

        The axis parameter tells numpy whether to sum across rows (axis=1) or columns (axis=0).
      2. Step 2: Clarify what axis does not affect

        It does not change data type, size, or repetition of sums.
      3. Final Answer:

        The direction along which the sum is calculated (rows or columns). -> Option B
      4. Quick Check:

        axis controls sum direction [OK]
      Hint: Axis decides row or column sum direction [OK]
      Common Mistakes:
      • Thinking axis changes data type
      • Confusing axis with array size
      • Assuming axis repeats sums
      2. Which of the following is the correct syntax to sum all elements of a 2D numpy array arr?
      easy
      A. np.sum(arr)
      B. np.sum(arr, axis=1, 0)
      C. np.sum(arr, axis=2)
      D. np.sum(arr, axis='all')

      Solution

      1. Step 1: Recall default behavior of np.sum()

        Without axis, np.sum(arr) sums all elements in the array.
      2. Step 2: Check invalid axis values

        axis=2 is invalid for 2D arrays, axis=1,0 is incorrect syntax, axis='all' is invalid.
      3. Final Answer:

        np.sum(arr) -> Option A
      4. Quick Check:

        Default np.sum sums all elements [OK]
      Hint: No axis sums all elements [OK]
      Common Mistakes:
      • Using invalid axis values
      • Passing multiple axis values incorrectly
      • Using string instead of integer for axis
      3. Given the array arr = np.array([[1, 2, 3], [4, 5, 6]]), what is the output of np.sum(arr, axis=0)?
      medium
      A. [5 7 9]
      B. [3 7]
      C. [6 15]
      D. [1 2 3 4 5 6]

      Solution

      1. Step 1: Understand axis=0 sums columns

        Summing along axis=0 adds elements vertically: (1+4), (2+5), (3+6).
      2. Step 2: Calculate sums for each column

        1+4=5, 2+5=7, 3+6=9, so result is [5 7 9].
      3. Final Answer:

        [5 7 9] -> Option A
      4. Quick Check:

        Sum columns = [5 7 9] [OK]
      Hint: axis=0 sums columns vertically [OK]
      Common Mistakes:
      • Confusing axis=0 with axis=1
      • Adding rows instead of columns
      • Expecting a scalar output
      4. What is wrong with this code snippet?
      import numpy as np
      arr = np.array([[1, 2], [3, 4]])
      result = np.sum(arr, axis=2)
      medium
      A. The array is not defined correctly.
      B. np.sum() cannot be used on 2D arrays.
      C. The axis value 2 is invalid for a 2D array.
      D. The import statement is missing.

      Solution

      1. Step 1: Check array dimensions

        The array has shape (2, 2), so valid axis values are 0 or 1.
      2. Step 2: Validate axis parameter

        axis=2 is out of bounds for this 2D array, causing an error.
      3. Final Answer:

        The axis value 2 is invalid for a 2D array. -> Option C
      4. Quick Check:

        Axis must be less than array dimensions [OK]
      Hint: Axis must be less than array dimensions [OK]
      Common Mistakes:
      • Using axis larger than array dimensions
      • Thinking np.sum can't handle 2D arrays
      • Ignoring error messages
      5. You have a 3D numpy array arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]]). What is the shape of the result when you run np.sum(arr, axis=1)?
      hard
      A. (1, 2)
      B. (3, 2)
      C. (2, 4)
      D. (2, 2)

      Solution

      1. Step 1: Understand the shape of arr

        arr has shape (2, 2, 2): 2 blocks, each with 2 rows and 2 columns.
      2. Step 2: Summing along axis=1

        axis=1 sums over the second dimension (rows), reducing it from 2 to 1, so shape changes from (2, 2, 2) to (2, 2).
      3. Final Answer:

        (2, 2) -> Option D
      4. Quick Check:

        Sum axis=1 reduces dimension 2 to 1 [OK]
      Hint: Sum along axis removes that dimension [OK]
      Common Mistakes:
      • Confusing which axis is summed
      • Expecting shape to stay same
      • Mixing up dimensions in 3D arrays
      Recommended

      Ready to prove it?

      Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

      10 questions~5 mineasy hard