Challenge - 5 Problems
Mean Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
โ Predict Output
intermediateOutput 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
intermediateNumber 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
advancedIdentify 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
advancedEffect 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
expertCalculate 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.
