Challenge - 5 Problems
Covariance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of np.cov() with two 1D arrays
What is the output of the following code snippet?
NumPy
import numpy as np x = np.array([1, 2, 3]) y = np.array([4, 5, 6]) cov_matrix = np.cov(x, y) print(cov_matrix)
Attempts:
2 left
💡 Hint
Recall that np.cov() by default calculates sample covariance with normalization by (N-1).
✗ Incorrect
The covariance matrix shows the covariance between x and y. Since both arrays increase linearly with the same step, covariance is 0.66666667. The sample covariance divides by (N-1) = 2, so the variance and covariance values are 0.66666667.
❓ data_output
intermediate1:30remaining
Shape of covariance matrix from 2D array input
Given the code below, what is the shape of the covariance matrix produced by np.cov()?
NumPy
import numpy as np data = np.array([[1, 2, 3], [4, 5, 6]]) cov_matrix = np.cov(data) print(cov_matrix.shape)
Attempts:
2 left
💡 Hint
np.cov() treats rows as variables and columns as observations by default.
✗ Incorrect
The input has 2 rows (variables) and 3 columns (observations). The covariance matrix shape is (number of variables, number of variables), so (2, 2).
🔧 Debug
advanced2:00remaining
Identify the error in covariance calculation
What error will this code raise, if any?
NumPy
import numpy as np x = [1, 2, 3] y = [4, 5] cov_matrix = np.cov(x, y) print(cov_matrix)
Attempts:
2 left
💡 Hint
Check if input arrays to np.cov() have the same length.
✗ Incorrect
np.cov() requires input arrays to have the same number of observations. Here, x has length 3 and y length 2, causing a ValueError.
🧠 Conceptual
advanced1:30remaining
Effect of rowvar parameter in np.cov()
Given a 2D array with shape (3, 4), what does setting rowvar=False do in np.cov()?
Attempts:
2 left
💡 Hint
rowvar=False changes the interpretation of rows and columns.
✗ Incorrect
By default, rowvar=True means each row is a variable. Setting rowvar=False means each column is a variable, so covariance is computed between columns.
🚀 Application
expert2:30remaining
Calculate covariance matrix for multiple variables
You have a dataset with 3 variables and 5 observations:
Variable A: [2, 4, 6, 8, 10]
Variable B: [1, 3, 5, 7, 9]
Variable C: [5, 7, 9, 11, 13]
Using np.cov(), what is the covariance between Variable A and Variable C?
NumPy
import numpy as np A = np.array([2, 4, 6, 8, 10]) B = np.array([1, 3, 5, 7, 9]) C = np.array([5, 7, 9, 11, 13]) data = np.array([A, B, C]) cov_matrix = np.cov(data) print(cov_matrix[0, 2])
Attempts:
2 left
💡 Hint
Calculate covariance manually or trust np.cov() output for these linear sequences.
✗ Incorrect
Variables A and C increase linearly with same step size. Covariance is calculated as sum((A-meanA)*(C-meanC))/(N-1). This equals 10.0.