Bird
Raised Fist0
NumPydata~20 mins

Covariance with np.cov() in NumPy - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Covariance Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2: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)
A
[[1. 1.]
 [1. 1.]]
B
[[0.66666667 0.66666667]
 [0.66666667 0.66666667]]
C
[[1.5 1.5]
 [1.5 1.5]]
D
[[2. 2.]
 [2. 2.]]
Attempts:
2 left
💡 Hint
Recall that np.cov() by default calculates sample covariance with normalization by (N-1).
❓ data_output
intermediate
1: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)
A(2, 2)
B(3, 3)
C(6, 6)
D(1, 1)
Attempts:
2 left
💡 Hint
np.cov() treats rows as variables and columns as observations by default.
🔧 Debug
advanced
2: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)
ANo error, outputs a covariance matrix
BTypeError: unsupported operand type(s) for -: 'list' and 'list'
CValueError: all the input arrays must have the same length
DIndexError: index out of range
Attempts:
2 left
💡 Hint
Check if input arrays to np.cov() have the same length.
🧠 Conceptual
advanced
1: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()?
ATreats columns as variables and rows as observations
BTreats rows as variables and columns as observations
CRaises an error because rowvar must be True
DReturns a covariance matrix with shape (4, 4) but with zeros
Attempts:
2 left
💡 Hint
rowvar=False changes the interpretation of rows and columns.
🚀 Application
expert
2: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])
A9.0
B8.0
C12.5
D10.0
Attempts:
2 left
💡 Hint
Calculate covariance manually or trust np.cov() output for these linear sequences.

Practice

(1/5)
1. What does the np.cov() function compute in numpy?
easy
A. The standard deviation of each variable
B. The correlation coefficient between two variables
C. The mean of the dataset
D. The covariance matrix showing how variables vary together

Solution

  1. Step 1: Understand covariance concept

    Covariance measures how two variables change together, indicating relationship strength and direction.
  2. Step 2: Identify np.cov() function purpose

    The np.cov() function calculates the covariance matrix from input data arrays.
  3. Final Answer:

    The covariance matrix showing how variables vary together -> Option D
  4. Quick Check:

    np.cov() = covariance matrix [OK]
Hint: np.cov() always returns covariance matrix [OK]
Common Mistakes:
  • Confusing covariance with correlation
  • Thinking np.cov() returns mean or std deviation
  • Assuming np.cov() returns a single number
2. Which of the following is the correct way to call np.cov() on two 1D arrays x and y?
easy
A. np.cov(x, axis=1)
B. np.cov(x, y)
C. np.cov(x + y)
D. np.cov([x + y])

Solution

  1. Step 1: Understand input format for np.cov()

    np.cov(m, y=None) where for two variables, pass two 1D arrays as m=x, y=y.
  2. Step 2: Check correct input structure

    Passing x, y directly uses the two-argument form of np.cov().
  3. Final Answer:

    np.cov(x, y) -> Option B
  4. Quick Check:

    np.cov(x, y) for two 1D arrays [OK]
Hint: np.cov(x, y) for two 1D arrays [OK]
Common Mistakes:
  • Passing summed arrays
  • Using wrong axis parameter
  • Passing list containing summed array
3. What is the output of this code?
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)
medium
A. [[1.5 1.5] [1.5 1.5]]
B. [[0.66666667 0.66666667] [0.66666667 0.66666667]]
C. [[1. 1.] [1. 1.]]
D. [[2. 2.] [2. 2.]]

Solution

  1. Step 1: Calculate covariance of x and y

    x mean=2, deviations=[-1,0,1], sum of squared deviations=2, np.cov uses ddof=1 by default (unbiased estimator), so variance = 2/(3-1) = 1. Same for covariance.
  2. Step 2: Understand np.cov() output

    np.cov([x, y]) returns 2x2 matrix with 1.0 everywhere.
  3. Final Answer:

    [[1. 1.] [1. 1.]] -> Option C
  4. Quick Check:

    np.cov uses ddof=1 by default (unbiased) [OK]
Hint: np.cov() divides by (n-1) by default [OK]
Common Mistakes:
  • Using biased variance (/n=3)
  • Misreading np.cov() output shape
  • Assuming default normalization is biased
4. Identify the error in this code snippet:
import numpy as np
x = np.array([1, 2, 3])
y = np.array([4, 5, 6])
z = np.array([7, 8, 9])
cov_matrix = np.cov(x, y, z)
print(cov_matrix)
medium
A. np.cov() requires a single array or a list of arrays, not separate arrays
B. x and y must be reshaped to 2D arrays before np.cov()
C. np.cov() cannot handle integer arrays
D. Missing axis parameter in np.cov() call

Solution

  1. Step 1: Check np.cov() input requirements

    np.cov() expects either a single 2D array or a list/array of variables, not multiple separate arrays as arguments.
  2. Step 2: Identify correct input format

    Passing x, y, z as separate arguments causes TypeError; correct is np.cov([x, y, z]).
  3. Final Answer:

    np.cov() requires a single array or a list of arrays, not separate arrays -> Option A
  4. Quick Check:

    Input must be single array or list [OK]
Hint: Pass variables as list, not separate args to np.cov() [OK]
Common Mistakes:
  • Passing multiple arrays as separate arguments
  • Assuming np.cov() auto-stacks inputs
  • Ignoring error messages about input shape
5. You have two datasets:
data1 = np.array([2, 4, 6, 8])
data2 = np.array([1, 3, 5, 7])

You want to find the covariance matrix but only for the first three elements of each dataset. Which code correctly computes this?
hard
A. np.cov(np.vstack((data1[:3], data2[:3])))
B. np.cov([data1[:3], data2])
C. np.cov(data1[0:3], data2)
D. np.cov(data1, data2)[:3, :3]

Solution

  1. Step 1: Select first three elements from each dataset

    Use slicing data1[:3] and data2[:3] to get first three elements.
  2. Step 2: Prepare input for np.cov()

    np.cov() expects 2D array with variables as rows; stacking with np.vstack() creates correct shape.
  3. Step 3: Compute covariance matrix

    Calling np.cov(np.vstack((data1[:3], data2[:3]))) computes covariance matrix for sliced data correctly.
  4. Final Answer:

    np.cov(np.vstack((data1[:3], data2[:3]))) -> Option A
  5. Quick Check:

    Stack sliced arrays then np.cov() [OK]
Hint: Stack sliced arrays with vstack before np.cov() [OK]
Common Mistakes:
  • Passing separate arrays without stacking
  • Slicing after calling np.cov()
  • Using incorrect slicing syntax
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard