0
0
NumPydata~20 mins

np.sum() and axis parameter in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sum Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[6 8 10]
B[5 7 9]
C[3 7 11]
D[1 2 3 4 5 6]
Attempts:
2 left
💡 Hint
axis=0 sums down the rows for each column.
Predict Output
intermediate
2: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))
A[26 29]
B[7 9 11]
C[15 19 23]
D[7 8 9 10 11 12]
Attempts:
2 left
💡 Hint
axis=1 sums across the columns for each row.
data_output
advanced
2:30remaining
Shape of result after np.sum with axis=1
Given this array:
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)
A(2, 2)
B(2, 1, 2)
C(1, 2, 2)
D(2, 4)
Attempts:
2 left
💡 Hint
Summing over axis=1 removes that dimension.
visualization
advanced
3:00remaining
Visualize np.sum() with axis=0 and axis=1
You have this 2D array:
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()
ALeft plot bars: [3,7,11], Right plot bars: [9,12]
BLeft plot bars: [3,7,11], Right plot bars: [6,15]
CLeft plot bars: [1,3,5], Right plot bars: [2,4,6]
DLeft plot bars: [9,12], Right plot bars: [3,7,11]
Attempts:
2 left
💡 Hint
Sum axis=0 adds columns, sum axis=1 adds rows.
🧠 Conceptual
expert
2: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?
AAn array with the same shape as the original, unchanged
BA 2D array with shape (3, 4) summing over the last axis
CA 1D array with length 3 summing over the last two axes
DA single number representing the sum of all elements in the array
Attempts:
2 left
💡 Hint
axis=None means sum everything into one value.