Challenge - 5 Problems
Mean Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.mean() on 2D array with axis=0
What is the output of this code snippet using
np.mean() on a 2D array with axis=0?NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = np.mean(arr, axis=0) print(result)
Attempts:
2 left
💡 Hint
Remember, axis=0 means averaging down the rows for each column.
✗ Incorrect
The mean along axis=0 calculates the average of each column: (1+4)/2=2.5, (2+5)/2=3.5, (3+6)/2=4.5.
❓ data_output
intermediate1:30remaining
Number of elements in np.mean() output with axis=1
Given a 3x4 numpy array, how many elements does the output have after applying
np.mean() with axis=1?NumPy
import numpy as np arr = np.arange(12).reshape(3,4) result = np.mean(arr, axis=1) print(result) print(len(result))
Attempts:
2 left
💡 Hint
Axis=1 means averaging across columns for each row.
✗ Incorrect
The array has 3 rows, so averaging across columns (axis=1) produces 3 mean values, one per row.
🔧 Debug
advanced2:00remaining
Identify the error in np.mean() usage
What error does this code raise when trying to compute the mean?
NumPy
import numpy as np arr = np.array([1, 2, 3]) result = np.mean(arr, axis=1) print(result)
Attempts:
2 left
💡 Hint
Check the shape of the array and the axis parameter.
✗ Incorrect
The array is 1D, so axis=1 is invalid and causes an AxisError.
🧠 Conceptual
advanced1:30remaining
Effect of np.mean() with keepdims=True
What is the shape of the output when applying
np.mean() on a 2D array with axis=1 and keepdims=True?NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = np.mean(arr, axis=1, keepdims=True) print(result.shape)
Attempts:
2 left
💡 Hint
Keepdims keeps the reduced dimension as size 1.
✗ Incorrect
With keepdims=True, the output keeps the axis dimension with size 1, so shape is (2,1).
🚀 Application
expert2:30remaining
Calculate weighted average using np.mean()
You have values
[10, 20, 30] and weights [1, 2, 3]. Which code correctly calculates the weighted average using numpy?Attempts:
2 left
💡 Hint
np.mean() does not support weights parameter; use np.average or manual calculation.
✗ Incorrect
np.mean() does not accept weights. np.average supports weights. Option D manually computes weighted average correctly.