Challenge - 5 Problems
Spread Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.std() with axis parameter
What is the output of the following code snippet?
NumPy
import numpy as np arr = np.array([[1, 2, 3], [4, 5, 6]]) result = np.std(arr, axis=0) print(result)
Attempts:
2 left
💡 Hint
Remember that np.std calculates the standard deviation along the specified axis.
✗ Incorrect
The standard deviation along axis 0 is calculated for each column. Each column has mean 2.5, sum of squared differences 4.5, divided by 2 (ddof=0) gives variance 2.25, std = 1.5.
❓ data_output
intermediate1:30remaining
Variance of a 1D array
What is the output of this code?
NumPy
import numpy as np arr = np.array([2, 4, 4, 4, 5, 5, 7, 9]) print(np.var(arr))
Attempts:
2 left
💡 Hint
Variance is the average of squared differences from the mean.
✗ Incorrect
The variance of the array is 3.0, calculated as the average squared distance from the mean.
🔧 Debug
advanced2:00remaining
Identify the error in variance calculation
What error will this code raise?
NumPy
import numpy as np arr = np.array([1, 2, 3]) result = np.var(arr, ddof=2) print(result)
Attempts:
2 left
💡 Hint
Check the degrees of freedom parameter and its effect on variance calculation.
✗ Incorrect
Using ddof=2 with 3 elements gives degrees of freedom = 3-2=1 >0, so no warning or error. Mean=2, sum of squared differences=2, variance=2/1=2.0.
🚀 Application
advanced2:30remaining
Choosing between np.std() and np.var() for data spread
You want to compare the spread of two datasets visually. Which numpy function is better to use for direct comparison and why?
Attempts:
2 left
💡 Hint
Think about the units of measurement for variance and standard deviation.
✗ Incorrect
Standard deviation is in the same units as the data, making it easier to interpret and compare visually. Variance is squared units, which can be confusing.
🧠 Conceptual
expert3:00remaining
Effect of axis and ddof on np.var() output shape
Given a 3D numpy array with shape (2,3,4), what is the shape of the output when calling np.var(arr, axis=(1,2), ddof=1)?
Attempts:
2 left
💡 Hint
Think about how variance reduces dimensions along specified axes.
✗ Incorrect
Variance computed along axes 1 and 2 reduces those dimensions, leaving axis 0 intact, so output shape is (2,).