Problems
Loading...
1 / 1

Compute Covariance Matrix

Linear AlgebraData Processing
Easy

Compute the covariance matrix from a dataset without using np.cov. The covariance matrix shows how features vary together and is fundamental to many ML algorithms.

Step 1: Center the Data

μ=mean(X,axis=0)Xcentered=Xμ\mu = \text{mean}(X, \text{axis} = 0) \\ X_{\text{centered}} = X - \mu

Step 2: Compute Covariance Matrix

Σ=1N1XcenteredTXcentered\Sigma = \frac{1}{N - 1} X_{\text{centered}}^T X_{\text{centered}}

Where: X has shape (N, D), μ has shape (D,), Σ has shape (D, D)

Function Arguments

  • X: list[list[float]] | np.ndarray - Dataset with shape (N, D)
Loading visualization...

Examples

Input: X=[[1, 2], [2, 3], [3, 4]]

Output: [[1.0, 1.0], [1.0, 1.0]]

Input: X=[[1, 0], [0, 1]]

Output: [[0.5, -0.5], [-0.5, 0.5]]

Input: X=[[1, 2, 3]]

Output: None (only 1 sample)

Hint 1

Use np.asarray() to convert input and check shape with .shape and .ndim. Use np.mean() to compute feature means.

Hint 2

Center data by subtracting mean for matrix multiplication.

Hint 3

Divide by (N-1) for sample covariance and handle edge cases by returning None.

Requirements

  • Return np.ndarray of shape (D, D) with covariance values
  • Return None for invalid input (N < 2 or not 2D)
  • Must be vectorized (no loops over data points)
  • Cannot use np.cov function
  • Use sample covariance (divide by N-1, not N)

Constraints

  • Dataset size: N ≤ 10,000, D ≤ 1,000
  • Numerical precision: relative tolerance ≤ 1e-8
  • Libraries: NumPy only
Try Similar Problems