Compute the Pearson correlation matrix without using np.corrcoef. Begin with the sample covariance matrix Σ and the feature standard deviations σi:
Rij=σiσjΣijHere, Rij is the correlation between features i and j. If either feature has zero variance, the corresponding correlation is NaN, including its diagonal entry. Return the full matrix as a NumPy array.
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]]
Compute covariance from centered data with centered.T @ centered / (N - 1).
Use np.sqrt(np.diag(covariance)) and np.outer() to build the denominator.
Sign in to take notes on this problem
Accepts: array
Compute the Pearson correlation matrix without using np.corrcoef. Begin with the sample covariance matrix Σ and the feature standard deviations σi:
Rij=σiσjΣijHere, Rij is the correlation between features i and j. If either feature has zero variance, the corresponding correlation is NaN, including its diagonal entry. Return the full matrix as a NumPy array.
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]]
Compute covariance from centered data with centered.T @ centered / (N - 1).
Use np.sqrt(np.diag(covariance)) and np.outer() to build the denominator.
Sign in to take notes on this problem
Accepts: array