TensorTonicTensorTonic
Problems
Study PlansProjectsNewInterviewPricingFeedback
Problems
Loading...
1 / 1

Compute Covariance Matrix

Linear AlgebraData Processing
Easy

Compute the sample covariance matrix without using np.cov. First center each feature:

Xc=X−μX_c = X - \muXc​=X−μ

Then compute:

Σ=XcTXcN−1\Sigma = \frac{X_c^{\mathsf T}X_c}{N-1}Σ=N−1XcT​Xc​​

Here, XXX has NNN samples and DDD features, μ\muμ is the vector of feature means, XcX_cXc​ is the centered data, and Σ\SigmaΣ is the D×DD \times DD×D sample covariance matrix. Return Σ\SigmaΣ as a NumPy array.

Loading visualization...

Examples

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

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

Explanation: Both features vary together by the same amount after centering.

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

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

Hint 1

Use X - np.mean(X, axis=0) to center every feature.

Hint 2

Use centered.T @ centered before dividing by X.shape[0] - 1.

Requirements

  • Center each feature independently
  • Divide by N minus 1 for sample covariance
  • Do not use np.cov
  • Return a NumPy array of shape (D,D)(D,D)(D,D)

Constraints

  • X is a rectangular numeric list with shape (N,D)(N,D)(N,D)
  • 2≤N≤10,0002 \le N \le 10{,}0002≤N≤10,000
  • Use NumPy only
Try Similar Problems
Pca ProjectionPearson CorrelationEigenvaluesMatrix NormalizationMatrix Inverse

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.
PrevNext

Compute Covariance Matrix

Linear AlgebraData Processing
Easy

Compute the sample covariance matrix without using np.cov. First center each feature:

Xc=X−μX_c = X - \muXc​=X−μ

Then compute:

Σ=XcTXcN−1\Sigma = \frac{X_c^{\mathsf T}X_c}{N-1}Σ=N−1XcT​Xc​​

Here, XXX has NNN samples and DDD features, μ\muμ is the vector of feature means, XcX_cXc​ is the centered data, and Σ\SigmaΣ is the D×DD \times DD×D sample covariance matrix. Return Σ\SigmaΣ as a NumPy array.

Loading visualization...

Examples

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

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

Explanation: Both features vary together by the same amount after centering.

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

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

Hint 1

Use X - np.mean(X, axis=0) to center every feature.

Hint 2

Use centered.T @ centered before dividing by X.shape[0] - 1.

Requirements

  • Center each feature independently
  • Divide by N minus 1 for sample covariance
  • Do not use np.cov
  • Return a NumPy array of shape (D,D)(D,D)(D,D)

Constraints

  • X is a rectangular numeric list with shape (N,D)(N,D)(N,D)
  • 2≤N≤10,0002 \le N \le 10{,}0002≤N≤10,000
  • Use NumPy only
Try Similar Problems
Pca ProjectionPearson CorrelationEigenvaluesMatrix NormalizationMatrix Inverse

Sign in to take notes on this problem

Case 1
Case 2

Accepts: array

You must run your code first.