0
0
NumPydata~20 mins

np.mean() for average in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Mean Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[2.5 3.5 4.5]
B[2. 3. 4.]
C[3. 4. 5.]
D[1.5 2.5 3.5]
Attempts:
2 left
💡 Hint
Remember, axis=0 means averaging down the rows for each column.
data_output
intermediate
1: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))
A12
B3
C4
D1
Attempts:
2 left
💡 Hint
Axis=1 means averaging across columns for each row.
🔧 Debug
advanced
2: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)
ATypeError: unsupported operand type(s) for +: 'int' and 'str'
BValueError: could not broadcast input array from shape (3,) into shape (1,)
CAxisError: axis 1 is out of bounds for array of dimension 1
DNo error, prints 2.0
Attempts:
2 left
💡 Hint
Check the shape of the array and the axis parameter.
🧠 Conceptual
advanced
1: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)
A(2, 1)
B(2,)
C(1, 3)
D(3, 2)
Attempts:
2 left
💡 Hint
Keepdims keeps the reduced dimension as size 1.
🚀 Application
expert
2: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?
Anp.mean([10, 20, 30], weights=[1, 2, 3])
Bnp.average([10, 20, 30], weights=[1, 2, 3])
Cnp.mean([10, 20, 30]) * np.mean([1, 2, 3])
Dnp.sum(np.array([10, 20, 30]) * np.array([1, 2, 3])) / np.sum([1, 2, 3])
Attempts:
2 left
💡 Hint
np.mean() does not support weights parameter; use np.average or manual calculation.