0
0
NumPydata~20 mins

np.std() and np.var() for spread in NumPy - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Spread Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A[1.11803399 1.11803399 1.11803399]
B[2.5 2.5 2.5]
C[2.0 2.0 2.0]
D[1.5 1.5 1.5]
Attempts:
2 left
💡 Hint
Remember that np.std calculates the standard deviation along the specified axis.
data_output
intermediate
1: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))
A3.0
B2.0
C4.0
D1.5
Attempts:
2 left
💡 Hint
Variance is the average of squared differences from the mean.
🔧 Debug
advanced
2: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)
ATypeError: ddof must be an integer
BRuntimeWarning: Degrees of freedom <= 0 for slice
CNo error, output is 2.0
DValueError: ddof must be less than or equal to the number of elements
Attempts:
2 left
💡 Hint
Check the degrees of freedom parameter and its effect on variance calculation.
🚀 Application
advanced
2: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?
Anp.var(), because variance is easier to interpret visually
Bnp.std(), because variance can be negative
Cnp.var(), because it is the square root of standard deviation
Dnp.std(), because it gives spread in the same units as the data
Attempts:
2 left
💡 Hint
Think about the units of measurement for variance and standard deviation.
🧠 Conceptual
expert
3: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)?
A(2,3,4)
B(2,)
C(1,)
D(3,4)
Attempts:
2 left
💡 Hint
Think about how variance reduces dimensions along specified axes.