Challenge - 5 Problems
Sum Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.sum() with axis=0 on 2D array
What is the output of the following code?
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = np.sum(arr, axis=0) print(result)
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
axis=0 sums down the rows for each column.
✗ Incorrect
np.sum with axis=0 adds elements vertically down each column. So, 1+4=5, 2+5=7, 3+6=9.
❓ Predict Output
intermediate2:00remaining
Result of np.sum() with axis=1 on 2D array
What does this code print?
import numpy as np arr = np.array([[7, 8], [9, 10], [11, 12]]) print(np.sum(arr, axis=1))
NumPy
import numpy as np arr = np.array([[7, 8], [9, 10], [11, 12]]) print(np.sum(arr, axis=1))
Attempts:
2 left
💡 Hint
axis=1 sums across the columns for each row.
✗ Incorrect
Summing along axis=1 adds elements horizontally in each row: 7+8=15, 9+10=19, 11+12=23.
❓ data_output
advanced2:30remaining
Shape of result after np.sum with axis=1
Given this array:
What is the shape of
import numpy as np arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]]) result = np.sum(arr, axis=1) print(result) print(result.shape)
What is the shape of
result?NumPy
import numpy as np arr = np.array([[[1,2],[3,4]], [[5,6],[7,8]]]) result = np.sum(arr, axis=1) print(result) print(result.shape)
Attempts:
2 left
💡 Hint
Summing over axis=1 removes that dimension.
✗ Incorrect
The original array shape is (2, 2, 2). Summing over axis=1 collapses the second dimension, resulting in shape (2, 2).
❓ visualization
advanced3:00remaining
Visualize np.sum() with axis=0 and axis=1
You have this 2D array:
Which plot correctly shows the sums along axis=0 and axis=1?
import numpy as np import matplotlib.pyplot as plt arr = np.array([[1, 3, 5], [2, 4, 6]])
Which plot correctly shows the sums along axis=0 and axis=1?
NumPy
import numpy as np import matplotlib.pyplot as plt arr = np.array([[1, 3, 5], [2, 4, 6]]) sum_axis0 = np.sum(arr, axis=0) sum_axis1 = np.sum(arr, axis=1) plt.figure(figsize=(8,4)) plt.subplot(1,2,1) plt.bar(['col0','col1','col2'], sum_axis0, color='blue') plt.title('Sum along axis=0') plt.subplot(1,2,2) plt.bar(['row0','row1'], sum_axis1, color='green') plt.title('Sum along axis=1') plt.tight_layout() plt.show()
Attempts:
2 left
💡 Hint
Sum axis=0 adds columns, sum axis=1 adds rows.
✗ Incorrect
Sum along axis=0: 1+2=3, 3+4=7, 5+6=11. Sum along axis=1: 1+3+5=9, 2+4+6=12.
🧠 Conceptual
expert2:30remaining
Effect of axis=None in np.sum() on 3D array
Consider a 3D numpy array with shape (3, 4, 5). What does
np.sum(arr, axis=None) return?Attempts:
2 left
💡 Hint
axis=None means sum everything into one value.
✗ Incorrect
When axis=None, np.sum adds all elements in the entire array, returning a single scalar number.