Skip to content

Instantly share code, notes, and snippets.

@wiso
wiso / covariance_to_correlation.py
Created March 20, 2018 08:33
Compute correlation matrix from covariance matrix using numpy
import numpy as np
def correlation_from_covariance(covariance):
v = np.sqrt(np.diag(covariance))
outer_v = np.outer(v, v)
correlation = covariance / outer_v
correlation[covariance == 0] = 0
return correlation