Challenge - 5 Problems
Aggregation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of numpy aggregation with axis
What is the output of this code that uses numpy to sum values along axis 0?
NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = np.sum(arr, axis=0) print(result)
Attempts:
2 left
💡 Hint
Think about summing down the rows for each column.
✗ Incorrect
Summing along axis 0 adds elements vertically, so each column's values are added: [1+4, 2+5, 3+6] = [5,7,9].
❓ data_output
intermediate2:00remaining
Number of elements after aggregation
After applying np.mean(arr, axis=1) on the array below, how many elements does the resulting array have?
NumPy
import numpy as np arr = np.array([[10, 20, 30], [40, 50, 60], [70, 80, 90]]) result = np.mean(arr, axis=1) print(result)
Attempts:
2 left
💡 Hint
Mean along axis 1 reduces columns, keeping rows count.
✗ Incorrect
Mean along axis 1 calculates average of each row, so output length equals number of rows, which is 3.
❓ visualization
advanced3:00remaining
Visualizing aggregation effect on data
Which plot correctly shows the effect of np.cumsum on the array below?
NumPy
import numpy as np import matplotlib.pyplot as plt arr = np.array([1, 2, 3, 4, 5]) cumsum = np.cumsum(arr) plt.plot(arr, label='Original') plt.plot(cumsum, label='Cumulative Sum') plt.legend() plt.show()
Attempts:
2 left
💡 Hint
Cumulative sum adds all previous values, so it grows faster than original.
✗ Incorrect
The cumulative sum plot rises faster than the original because each point adds all previous values, showing aggregation effect.
🧠 Conceptual
advanced2:00remaining
Why aggregation reduces data size
Why does aggregation like np.sum or np.mean reduce the size of data when applied along an axis?
Attempts:
2 left
💡 Hint
Think about what sum or mean does to multiple numbers.
✗ Incorrect
Aggregation functions combine many values into one summary value per group or axis, reducing the number of elements.
🔧 Debug
expert2:00remaining
Identify the error in aggregation code
What error will this code raise and why?
NumPy
import numpy as np arr = np.array([[1, 2], [3, 4]]) result = np.sum(arr, axis=2) print(result)
Attempts:
2 left
💡 Hint
Check the dimensions of the array and the axis argument.
✗ Incorrect
The array has 2 dimensions (axes 0 and 1). Axis 2 does not exist, so numpy raises AxisError.