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

Compute Pearson Correlation Matrix

Data ProcessingLinear Algebra
Medium

Compute the Pearson correlation matrix without using np.corrcoef. Begin with the sample covariance matrix Σ\SigmaΣ and the feature standard deviations σi\sigma_iσi​:

Rij=ΣijσiσjR_{ij} = \frac{\Sigma_{ij}}{\sigma_i\sigma_j}Rij​=σi​σj​Σij​​

Here, RijR_{ij}Rij​ is the correlation between features iii and jjj. If either feature has zero variance, the corresponding correlation is NaN, including its diagonal entry. Return the full matrix as a NumPy array.

Loading visualization...

Examples

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

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

Explanation: Both features increase together, but their relationship is not perfectly proportional.

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

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

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

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

Hint 1

Compute covariance from centered data with centered.T @ centered / (N - 1).

Hint 2

Use np.sqrt(np.diag(covariance)) and np.outer() to build the denominator.

Requirements

  • Compute sample covariance without np.corrcoef
  • Normalize covariance by the outer product of feature standard deviations
  • Return NaN where a feature has zero variance
  • 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
Covariance MatrixMatrix NormalizationSample Var StdLinear Regression Closed FormBootstrap Mean

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

You must run your code first.
PrevNext

Compute Pearson Correlation Matrix

Data ProcessingLinear Algebra
Medium

Compute the Pearson correlation matrix without using np.corrcoef. Begin with the sample covariance matrix Σ\SigmaΣ and the feature standard deviations σi\sigma_iσi​:

Rij=ΣijσiσjR_{ij} = \frac{\Sigma_{ij}}{\sigma_i\sigma_j}Rij​=σi​σj​Σij​​

Here, RijR_{ij}Rij​ is the correlation between features iii and jjj. If either feature has zero variance, the corresponding correlation is NaN, including its diagonal entry. Return the full matrix as a NumPy array.

Loading visualization...

Examples

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

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

Explanation: Both features increase together, but their relationship is not perfectly proportional.

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

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

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

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

Hint 1

Compute covariance from centered data with centered.T @ centered / (N - 1).

Hint 2

Use np.sqrt(np.diag(covariance)) and np.outer() to build the denominator.

Requirements

  • Compute sample covariance without np.corrcoef
  • Normalize covariance by the outer product of feature standard deviations
  • Return NaN where a feature has zero variance
  • 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
Covariance MatrixMatrix NormalizationSample Var StdLinear Regression Closed FormBootstrap Mean

Sign in to take notes on this problem

Case 1
Case 2
Case 3

Accepts: array

You must run your code first.