0
0
NumPydata~20 mins

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

Choose your learning style9 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.