Challenge - 5 Problems
Aggregation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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)
Attempts:
2 left
💡 Hint
Sum along axis 0 means summing down each column.
✗ Incorrect
Summing along axis 0 adds elements vertically: [1+4, 2+5, 3+6] = [5, 7, 9].
❓ data_output
intermediate2: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)
Attempts:
2 left
💡 Hint
Mean along axis 1 means averaging each row.
✗ Incorrect
Each row's mean: (1+2+3)/3=2, (4+5+6)/3=5, (7+8+9)/3=8.
❓ visualization
advanced3: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()
Attempts:
2 left
💡 Hint
Sum along axis 0 sums columns; axis 1 sums rows.
✗ Incorrect
Sum axis 0: [1+4, 2+5, 3+6] = [5,7,9]. Sum axis 1: [1+2+3, 4+5+6] = [6,15].
🔧 Debug
advanced2: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)
Attempts:
2 left
💡 Hint
Check the number of dimensions of the array and the axis argument.
✗ Incorrect
The array has 2 dimensions (axes 0 and 1). Axis 2 does not exist, causing AxisError.
🚀 Application
expert3: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
Attempts:
2 left
💡 Hint
Sum over regions and months to get totals per product.
✗ Incorrect
Summing over axis 0 (regions) and axis 2 (months) leaves axis 1 (products) summed per product.