0
0
NumPydata~20 mins

Why aggregation matters in NumPy - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Aggregation Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[1 2 3 4 5 6]
B[5 7 9]
C[6 15]
D[3 7 11]
Attempts:
2 left
💡 Hint
Think about summing down the rows for each column.
data_output
intermediate
2: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)
A3
B1
C9
D6
Attempts:
2 left
💡 Hint
Mean along axis 1 reduces columns, keeping rows count.
visualization
advanced
3: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()
ALine plot with original values increasing by 1 and cumulative sum increasing faster
BPie chart showing distribution of original values
CScatter plot with original values decreasing and cumulative sum decreasing
DBar plot with original values and cumulative sum as constant line
Attempts:
2 left
💡 Hint
Cumulative sum adds all previous values, so it grows faster than original.
🧠 Conceptual
advanced
2: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?
ABecause aggregation removes all rows and columns except the first
BBecause aggregation duplicates data to fill missing values
CBecause aggregation combines multiple values into a single summary value along the specified axis
DBecause aggregation sorts data and removes duplicates
Attempts:
2 left
💡 Hint
Think about what sum or mean does to multiple numbers.
🔧 Debug
expert
2: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)
AValueError: operands could not be broadcast together with shapes (2,2) (2,)
BTypeError: unsupported operand type(s) for +: 'int' and 'str'
CNo error, prints [3 7]
DAxisError: axis 2 is out of bounds for array of dimension 2
Attempts:
2 left
💡 Hint
Check the dimensions of the array and the axis argument.