Bird
Raised Fist0
NumPydata~20 mins

Aggregation along specific axes in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Sum aggregation along axis in a 2D array
What is the output of the following code that sums elements along axis 0 of a 2D numpy array?
NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = arr.sum(axis=0)
print(result)
A[6 15]
B[3 7 11]
C[5 7 9]
D[1 2 3 4 5 6]
Attempts:
2 left
💡 Hint
Sum along axis 0 means summing down each column.
❓ data_output
intermediate
2:00remaining
Mean aggregation along axis 1 in a 3x3 array
What is the output array after computing the mean along axis 1 of this numpy array?
NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
result = arr.mean(axis=1)
print(result)
A[5. 5. 5.]
B[1. 4. 7.]
C[3. 6. 9.]
D[2. 5. 8.]
Attempts:
2 left
💡 Hint
Mean along axis 1 means averaging each row.
❓ visualization
advanced
3:00remaining
Visualizing sum aggregation along axis 0 and axis 1
Which option correctly describes the bar plots generated by summing a 2D array along axis 0 and axis 1?
NumPy
import numpy as np
import matplotlib.pyplot as plt
arr = np.array([[1, 2, 3], [4, 5, 6]])
sum_axis0 = arr.sum(axis=0)
sum_axis1 = arr.sum(axis=1)
plt.subplot(1,2,1)
plt.bar(range(len(sum_axis0)), sum_axis0)
plt.title('Sum along axis 0')
plt.subplot(1,2,2)
plt.bar(range(len(sum_axis1)), sum_axis1)
plt.title('Sum along axis 1')
plt.show()
ALeft plot bars heights: [5,7,9]; Right plot bars heights: [6,15]
BLeft plot bars heights: [6,15]; Right plot bars heights: [5,7,9]
CBoth plots have bars heights: [5,7,9]
DBoth plots have bars heights: [6,15]
Attempts:
2 left
💡 Hint
Sum along axis 0 sums columns; axis 1 sums rows.
🔧 Debug
advanced
2:00remaining
Identify the error in axis aggregation
What error will this code raise when trying to sum along axis 2 of a 2D numpy array?
NumPy
import numpy as np
arr = np.array([[1, 2, 3], [4, 5, 6]])
result = arr.sum(axis=2)
print(result)
AAxisError: axis 2 is out of bounds for array of dimension 2
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CValueError: operands could not be broadcast together with shapes (2,3) (3,2)
DNo error, output: [6 15]
Attempts:
2 left
💡 Hint
Check the number of dimensions of the array and the axis argument.
🚀 Application
expert
3:00remaining
Calculate total sales per product and per region
Given a 3D numpy array representing sales data with shape (regions, products, months), which code correctly computes total sales per product across all regions and months?
NumPy
import numpy as np
sales = np.array([
  [[10, 20], [30, 40]],  # Region 1
  [[5, 15], [25, 35]]    # Region 2
])
# Compute total sales per product across regions and months
result = None  # Fill in the correct aggregation
Asales.sum(axis=2)
Bsales.sum(axis=(0,2))
Csales.sum(axis=1)
Dsales.sum(axis=0)
Attempts:
2 left
💡 Hint
Sum over regions and months to get totals per product.

Practice

(1/5)
1. What does setting axis=0 do when aggregating a 2D NumPy array?
easy
A. Aggregates values down each column (per column)
B. Aggregates values across each row (per row)
C. Flattens the array before aggregation
D. Aggregates only the diagonal elements

Solution

  1. Step 1: Understand axis parameter meaning

    In NumPy, axis=0 means aggregation happens down the rows, combining values in each column.
  2. Step 2: Apply to 2D array

    Aggregating with axis=0 summarizes each column separately.
  3. Final Answer:

    Aggregates values down each column (per column) -> Option A
  4. Quick Check:

    axis=0 means down rows = per column aggregation [OK]
Hint: axis=0 aggregates columns, axis=1 aggregates rows [OK]
Common Mistakes:
  • Confusing axis=0 with axis=1
  • Thinking axis=0 aggregates rows instead of columns
  • Assuming axis=0 flattens the array
2. Which of the following is the correct syntax to sum a 2D NumPy array arr along rows (per row)?
easy
A. np.sum(arr, axis=1)
B. np.sum(arr, axis=0)
C. arr.sum(axis=2)
D. np.sum(arr)

Solution

  1. Step 1: Identify axis for row-wise sum

    Summing across columns (per row) requires axis=1.
  2. Step 2: Check syntax correctness

    np.sum(arr, axis=1) correctly sums each row's elements.
  3. Final Answer:

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

    Sum per row uses axis=1 [OK]
Hint: Use axis=1 to sum across columns (per row) [OK]
Common Mistakes:
  • Using axis=0 to sum rows
  • Using invalid axis like 2 for 2D array
  • Forgetting to specify 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. [3 7 11]
B. [5 7 9]
C. [6 15]
D. [1 2 3 4 5 6]

Solution

  1. Step 1: Understand axis=0 sum

    Sum down rows means add elements in each column: (1+4), (2+5), (3+6).
  2. Step 2: Calculate sums

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

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

    Sum axis=0 adds columns down rows [OK]
Hint: Sum axis=0 adds columns down rows [OK]
Common Mistakes:
  • Adding rows instead of columns
  • Confusing axis=0 with axis=1
  • Returning flattened array instead of summed
4. What is wrong with this code snippet?
arr = np.array([[1, 2], [3, 4]])
result = np.mean(arr, axis=2)
medium
A. No error, code runs fine
B. np.mean cannot be used on arrays
C. Missing parentheses in np.array
D. axis=2 is invalid for 2D array

Solution

  1. Step 1: Check array dimensions

    The array arr is 2D with shape (2,2), so valid axes are 0 and 1.
  2. Step 2: Validate axis parameter

    Using axis=2 is invalid and causes an error because axis 2 does not exist.
  3. Final Answer:

    axis=2 is invalid for 2D array -> Option D
  4. Quick Check:

    Axis must be less than array dimensions [OK]
Hint: Axis must be less than array dimensions [OK]
Common Mistakes:
  • Using axis number too large for array
  • Thinking np.mean can't be used on arrays
  • Ignoring error messages
5. You have a 3D NumPy array data with shape (4, 3, 2). You want to compute the sum of each 2D slice along the first axis (axis=0), resulting in a 2D array. Which code correctly does this?
hard
A. np.sum(data)
B. np.sum(data, axis=1)
C. np.sum(data, axis=0)
D. np.sum(data, axis=2)

Solution

  1. Step 1: Understand array shape and axis

    Array shape is (4,3,2). Summing along axis=0 collapses the first dimension (4), resulting in shape (3,2).
  2. Step 2: Apply sum along axis=0

    Using np.sum(data, axis=0) sums all 4 slices element-wise, producing a 2D array.
  3. Final Answer:

    np.sum(data, axis=0) -> Option C
  4. Quick Check:

    Sum axis=0 collapses first dimension to 2D [OK]
Hint: Sum axis=0 collapses first dimension in 3D arrays [OK]
Common Mistakes:
  • Using wrong axis and getting wrong shape
  • Summing all elements without axis
  • Confusing axis numbering 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